Django REST Framework and Model Serializer Guide

How to create CRUD REST APIs for a Django web application

Vikas Gautam
Python in Plain English

--

Django Rest Framework UI by Vikas Gautam

Django is a high population python framework for creating a scalable web application backend that can serve a high number of users. REST APIs are also very popular and they are used by a UI like react, angular, swagger, etc. REST APIs are used to send data from DB to UI and vise versa.

Here I will show you how you can create CRUD (create, retrieve, update and delete) REST APIs using Django Rest Framework and Django REST framework model serializer by taking an example of User Registration.

Now Let’s create a Django web application and User CRUD REST APIs

Create Django web application

  1. Create Virtual Environment and Activate it

Install: https://pypi.org/project/virtualenv/

virtualenv venv

Windows

cd venv/Scripts
activate

Linux

cd venv/bin
source activate

2. Install Django and Django REST Framework

Rest framework installation commands

3. Create Django project and app inside the project

Project creation commands

4. In settings.py

settings.py

5. In MyApis/models.py

models.py

6. In MyApis/serializers.py

serializers.py

7. In MyApis/views.py

views.py

8. In urls.py

urls.py

9. Make migrations And Run

commands

Now, you can test your APIs using Django REST Framework form or Postman

REST Framework

form URL http://127.0.0.1:8000/myapis/

Django Rest Framework UI

Postman

Post API

Get API

Put API

Delete API

Django REST Framework is easy to use and has very good documentation, for more check out https://www.django-rest-framework.org/

Here we have used Postman for accessing Rest APIs but Postman is mostly used by backend developers only who know how APIs work and what inputs they need.

Since mostly backend APIs are developed first and then these are used by frontend developers who do not know APIs details that’s why for them to access APIs they have to always to through API documentation or ask the developer itself which is waste of time and delays other ongoing tasks.

So to overcome this problem backend developers can integrate Swagger UI to Django Web Applications and frontend developers can use Swagger UI to check any Rest API. The benefit of Swagger is, here description and all input fields are pre-defined. users just need to execute APIs which their input values.

Check out this blog for integration of Swagger UI with Django

https://vgautam99.medium.com/d64878f1314f?source=friends_link&sk=b1a643ce475314266ce0c22ceb3a56d0

You will get all files used in this blog on my GitHub repo https://github.com/vgautam99/DjangoRestFramework

More content at plainenglish.io

--

--