Django REST Framework is a wrapper over default Django Framework, basically used to create APIs of various kinds. There are three stages before creating a API through REST framework, Converting a Model’s data to JSON/XML format (Serialization), Rendering this data to the view, Creating a URL for mapping to the viewset.
This article revolves around how to create a basic API using Django REST Framework. It assumes you are familiar with Django basics – Django tutorial. Also, installation of Django REST Framework. Assuming you have created a project named geeksforgeeks with Django, let’s initiate Django REST Framework.
Add rest_framework to INSTALLED_APPS
To initialize REST Framework in your project, go to
settings.py, and in INSTALLED_APPS add ‘rest_framework’ at the bottom.
Create a app and model
Now, let’s create a app using command,
python manage.py startapp apis
A folder with name apis would have been registered by now. let’s add this app to INSTALLED_APPS and urls.py also.
Now, add apis urls in urls.py. In geeksforgeeks.urls.py,
Create a model
To demonstrate, creating and using an API, let’s create a model named “GeeksModel”. In apis/models.py
now our app is ready, let’s serialize the data and create views from the same.
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data. Let’s start creating a serializer, in file
Creating a viewset
To render data into frontend, and handle requests from user, we need to create a view. In Django REST Framework, we call these as viewsets, so let’s create a view in
Define URLs of API
Specify the url path of APIs to be accessed, In
After everything is sucessfully ready, let’s run some commands to activate the server.
Run server and check API
Run following commands to create the database, and run server,
python manage.py makemigrations python manage.py migrate python manage.py runserver
Now visit http://127.0.0.1:8000/geeks/,
To check the code for the project, click here
- Django REST Framework Installation
- Serializers - Django REST Framework
- JWT Authentication with Django REST Framework
- IPAddressField in serializers - Django REST Framework
- ListField in serializers - Django REST Framework
- Creating and Using Serializers - Django REST Framework
- URL fields in serializers - Django REST Framework
- ModelSerializer in serializers - Django REST Framework
- HiddenField in serializers - Django REST Framework
- JSONField in serializers - Django REST Framework
- HyperlinkedModelSerializer in serializers - Django REST Framework
- Serializer Fields - Django REST Framework
- DictField in serializers - Django REST Framework
- Boolean Fields in Serializers - Django REST Framework
- String Fields in Serializers - Django REST Framework
- Implement Token Authentication using Django REST Framework
- Numeric fields in serializers - Django REST Framework
- File upload Fields in Serializers - Django REST Framework
- Choice Selection Fields in serializers - Django REST Framework
- Core arguments in serializer fields - Django REST Framework