Building REST API With Django Rest Framework

What actually is REST API?

  • GET — According to the endpoints and parameters that we pass, the API returns some data.
  • PUT — It looks for the record in the provided URI and updates the existing record. If the record is not present, it creates a new one.
  • POST — It creates and adds a new record in the database.
  • DELETE — It removes the requested record in the given URI.
  • PATCH — It updates the individual fields of a record.


$ pip install pipenv

1.1 Install Django and Configure Virtual Environment

$ pipenv install django
$ pipenv shell
$ django-admin startproject myrestapi
$ ls
myrestapi/ Pipfile Pipfile.lock
$ cd myrestapi
$ ls myrestapi/
$ python runserverWatching for file changes with StatReloaderPerforming system checks…System check identified no issues (0 silenced).
You have 18 unapplied migration(s).
Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python migrate' to apply them.November 27, 2020–06:46:53Django version 3.1.3, using settings 'myrestapi.settings'Starting development server at
the server with CONTROL-C.

1.2 Create API app

$ python startapp api
$ ls
api db.sqlite3 myrestapi/

1.3 Register the api app with the myrestapi project

1.4 Migrate the Database

$ python migrateOperations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial… OK 
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying auth.0012_alter_user_first_name_max_length… OK
Applying sessions.0001_initial… OK

1.5 Create a Superuser

$ python createsuperuserUsername (leave blank to use 'navraj'):  
Email address:
Password (again): This password is too common. Bypass password validation and create user anyway? [y/N]: y Superuser created successfully.
$ python runserver

2. Create a model in the api app

2.1 api/

2.2 Make migrations

$ python makemigrationsMigrations for 'api': api/migrations/ 
- Create model Detective$ python migrateOperations to perform: Apply all migrations: admin, api, auth, contenttypes, sessions Running migrations: Applying api.0001_initial… OK

2.3 Register Detective in the admin site

2.4 Create some cool Detectives

3. Set up Django Rest Framework

$ pipenv install djangorestframework

4. Now Serialize the Detective Model

5. View the Data


5.2 Project Level URLs

5.3 App-Level URLs

Now Enjoy the testing

Get The Individual Detectives With IDs

Add a new detective Via POST Request



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store