gaurav0401 / Working-with-Django-Rest-Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Working-with-Django-Rest-Framework

Django REST framework is a powerful and flexible toolkit for building Web APIs.

There are six stages for building an API using Django Rest Framework

  • Installing django and django rest framework.
  • creating Models
  • creating Serializers
  • creating views
  • creating urls
  • Our API is ready

Step by step guide:

  • Add 'rest_framework' in installed apps of settings.py

  • Create a new app using 'django-admin startapp app1' and then that app in installed apps of settings.py.

  • Create a new Model in models.py and add fields as your needs.

  • Now create a new file named serializers.py in app folder and import your model here.

  • create a serializer in serializers.py using Meta data of model:

  • image

  • Now import both model and serializer in views.py and create viewset of model: image

  • Now in urls.py of app import 'viewset' here and create a router using routers method of rest_framework

  • Now register that router using url and viewset (i.e router.register(r"url", viewsetname)) image

  • Now apply migrations

  • Now run server using 'python manage.py runserver' and go to url which we created for router (i.e /companies in our case). image

About