Django REST Framework Course for Beginners

In this article we are going to talk about Django REST Framework Course for Beginners, also for more information about Django REST Framework Course for Beginners you can check the complete video for this article.

 

 

What are RESTFull API’s

A RESTful API acts as a translator between two machines communicating over a Web service. If your familiar with the concept of API This is just like an API but it’s working on a RESTful Web service and the API is used by two applications trying to communicate with each other over a network or Internet.
Web developers program REST API that server can receive data from applications. These applications can be web-apps, Android/iOS apps, etc. RESTful APIs return JSON files which can be interpreted by a variety of devices.

 

 

 

 

What is Django REST Framework 

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

Some reasons you might want to use REST framework:

  • The Web browsable API is a huge usability win for your developers.
  • Authentication policies including packages for OAuth1a and OAuth2.
  • Serialization that supports both ORM and non-ORM data sources.
  • Customizable all the way down – just use regular function-based views if you don’t need the more powerful features.
  • Extensive documentation, and great community support.
  • Used and trusted by internationally recognised companies including Mozilla, Red Hat, Heroku, and Eventbrite.

 

 

 

Also you can read more django articles

1: Django Pagination Complete Example

2: Django Sending Email to Gmail Account 

3: Build News Application in Django 

4: Django Aggregation Example

5: Django User Authentication Framework 

 

 

 

Also you can watch the complete video for Django REST Framework Course for Beginners

 

 

 

Installation 

For the installation you can just use pip.

 

 

 

OK after installation we need to create a new project in Django, and iam going to give the project name as MyProject.

 

 

Now you need to change directory to the created project, after that create a new App, iam going to call my app as api_basic.

 

 

 

 

Also we need to create a superuser for our project like this.

 

 

 

 

Now you need to open your django settings.py and add django rest_framework with your created app in the INSTALLED_APP of setting.

 

 

The important concept in building api is serializer, now you need to create a new python file in your created app at name of serializer.py, right now we are not adding any thing to this file.

 

 

 

What is Serializer 

Before sending data to the client we need to serialize the data to JSON. the first thing we need to get started on our Web API is to provide a way of serializing and deserializing the data instances into representations such as json. We can do this by declaring serializers that work very similar to Django’s forms.

 

 

 

So now it is time to create our model, now you need to open your models.py file in your app, and add this code, basically we are going to create an Article model.

 

 

 

 

As in the top we have added the rest_framework to our installed app, also we have created a new model, now you need to migrate.

 

 

 

Now we can add our model in the admin.py, because we are using django super user for adding data.

 

 

 

OK now we are going to add codes in our serializer.py file, that we have already created. there are different serialzers that you can use, we will do this step by step.

 

 

 

 

If you are using this method, you need to specify all models fields to your seriaizer file.

 

The first part of the serializer class defines the fields that get serialized/deserialized. The create() and update() methods define how fully fledged instances are created or modified when calling serializer.save()

A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields, such as requiredmax_length and default.

 

 

 

 

OK now open django shell using this command.

 

 

 

 

So in django shell first of all you need these imports.

 

 

 

Now we need to create an article and save that.

 

If you check your django admin panel, you can see that i have a new article. also you can add as much as articles you want.

 

 

 

now let’s just serialize the article, we are going to again django shell.

 

 

 

 

At this point we’ve translated the model instance into Python native datatypes. To finalize the serialization process we render the data into json.

 

Now this is our serialized data.

 

 

 

 

We can also serialize querysets instead of model instances. To do so we simply add a many=True flag to the serializer arguments.

 

 

 

 

ModelSerializers

Our ArticleSerializer class is replicating a lot of information that’s also contained in the Article model. It would be nice if we could keep our code a bit more concise. In the same way that Django provides both Form classes and ModelForm classes, REST framework includes both Serializer classes, and ModelSerializer classes.

 

Let’s look at refactoring our serializer using the ModelSerializer class

 

 

Let’s see how we can write some API views using our new Serializer class. For the moment we won’t use any of REST framework’s other features, we’ll just write the views as regular Django views.

 

 

 

So now open your views.py, and add this code.

 

 

 

 

So now we are going to create a new python file in our app (api_basic) at name of urls.py.

 

 

 

 

Also you need to add this file to your main urls.py like this.

 

 

 

 

Now run the project

 

 

If you go to this url, you will see this result.

 

Django REST Framework
Django REST Framework

 

 

 

 

 

Also you can hit this url using PostMan.

Django REST Framework Course for Beginners
Django REST Framework Course for Beginners

 

 

Also you can post the articles. but make sure that you have added csrf_exempt at the top of your view function, as we have already done this.

 

 

 

We’ll also need a view which corresponds to an individual article, and can be used to retrieve, update or delete the article. so now open your views.py and we need to add a new view function like this. before this we have created the view function for get and post of the article, but this is for delete and updating the article. also make note that these are function based api view, but when we have used class based or generic views, we don’t use these codes.

 

 

 

Also you need to open your urls.py, and add this new view function to the urls.py.

 

 

 

Now using this you can retrieve an specific id article, or you can update an article or you can delete an article.

Django REST Framework Course for Beginners
Django REST Framework Course for Beginners

 

 

 

 

Using api_view() Decorator in Function Based View

Also you can use decorators with function api based view, for example you can use api_view(), the core of this functionality is the api_view decorator, which takes a list of HTTP methods that your view should respond to. also using this decorator you can access to django browsable api view.

 

 

Now we are going to change views.py functions like this.

 

 

 

 

If you go to http://localhost:8000/article/      you can see a browsable api from the django rest framework with same functionality of posting article, getting article, retrieving article, deleting article and updating article .

Django REST Framework Browsable API
Django REST Framework Browsable API

 

 

 

 

 

 

Class Based API View

OK till now we have learned some basics on django rest framework, and we have created some examples on functions based api view.  now it is time to talk about class based api view in django. REST framework provides an APIView class, which subclasses Django’s View class. Using the APIView class is pretty much the same as using a regular View class, as usual, the incoming request is dispatched to an appropriate handler method such as .get() or .post(). Additionally, a number of attributes may be set on the class that control various aspects of the API policy.

 

 

OK now we are going to bring changes in views.py again, i have removed all of the code from the views.py, because by using class based api view i don’t need that code.

 

 

 

 

Also you need to bring changes in your urls.py file , because now we are using class based views.

 

 

If you go to http://localhost:8000/article/      you can see a browsable api from the django rest framework with same functionality of posting article, getting article, retrieving article, deleting article and updating article . but this time we have used class based api view.

 

Django Class Based API View
Django Class Based API View

 

 

 

 

Generic View And Mixins

One of the key benefits of class-based views is the way they allow you to compose bits of reusable behavior. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns. the generic views provided by REST framework allow you to quickly build API views that map closely to your database models. if the generic views don’t suit the needs of your API, you can drop down to using the regular APIView class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views.

 

 

 

OK now we are again changing our views.py, and we are removing the previous code, by using this code you can add, get, update and delete the data. also there are separate mixins for different functionalities, for example posting article we are going to use CreateModelMixin, for getting the articles we are using ListModelMixin, for updating we are using UpdateModelMixin and for deleting we can use DestroyModelMixin.

 

 

 

Also you need to change your urls.py file

 

 

If you go to http://localhost:8000/generic/article/    you can see a browsable api from the django rest framework with same functionality of posting article, getting article, retrieving article, deleting article and updating article . but this time we have used generic view and mixins.

 

 

 

Authentications

Authentication is the mechanism of associating an incoming request with a set of identifying credentials, such as the user the request came from, or the token that it was signed with. The permission and throttling policies can then use those credentials to determine if the request should be permitted. REST framework provides a number of authentication schemes out of the box, and also allows you to implement custom schemes. Authentication is always run at the very start of the view, before the permission and throttling checks occur, and before any other code is allowed to proceed. there are different authentications that we can use for example, Basic Authentication, Token Authentication and Session Authentication.

 

 

  • Basic Authentication: This authentication scheme uses HTTP Basic Authentication, signed against a user’s username and password. Basic authentication is generally only appropriate for testing.
  • Token Authentication: This authentication scheme uses a simple token-based HTTP Authentication scheme. Token authentication is appropriate for client-server setups, such as native desktop and mobile clients.
  • Session Authentication: This authentication scheme uses Django’s default session backend for authentication. Session authentication is appropriate for AJAX clients that are running in the same session context as your website.

 

 

for settings of the authentication, you can use globally or you can use inside your python file. for the globally you can add the DEFAULT_AUTHENTICATION_CLASSES in your settings.py file.

 

 

Note: we are not using globally, but we are adding the authentication classes inside our python file.

 

 

So now we are changing our views.py and we are adding our authentication classes.

 

First you need to import the required classes.

 

 

 

 

And now this is our new code for views.py

 

 

I have commented the token authentication but you can use like that, now in here we have used session and basic authentication together, it will first check for the session authentication and after that check for basic authentication.

 

 

If you go to http://localhost:8000/generic/article/6/ ,   you can see a browsable api from the django rest framework. but this time we can not access to data, because we have used authentication.

 

Django Authentication Framework
Django Authentication Framework

 

 

For token authentication, you can manually create token from your django admin panel and after that you can authenticate using token.

 

 

 

Viewsets And Routers

ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list() and .create(). the method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the .as_view() method.

 

There are different ways that you can implement viewsets, first way is that you can write your own viewsets.  now we are going to change our views.py.

 

 

 

 

Also you need to add your routers.

 

 

 

 

 

If you go to http://localhost:8000/viewset/article/  you can see a browsable api from the django rest framework with same functionality, but this time we have used viewsets and routers.

Django REST Viewsets and Routers
Django REST Viewsets and Routers

 

 

I have not used the update and delete functionalities, but you can do by your own.

 

 

Rather than writing your own viewsets, you’ll often want to use the existing base classes that provide a default set of behavior. For example:

 

 

 

 

 

 

 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

5 thoughts on “Django REST Framework Course for Beginners”

  1. Hey Parwiz

    Very thorough and well explained.

    One bug. In GenericAPI section, POST request won’t work because the post method isn’t expecting an id argument that is being passed in via the URL.
    This can be easily fixed by adding an id=None argument to the post request in the GenericAPIView class as below:

    class GenericAPIView(generics.GenericAPIView,
    mixins.ListModelMixin,
    mixins.CreateModelMixin,
    mixins.UpdateModelMixin,
    mixins.RetrieveModelMixin,
    mixins.DestroyModelMixin):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()

    lookup_field = 'id'

    def get(self, request, id=None):

    if id:
    return self.retrieve(request)
    else:
    return self.list(request)

    def post(self, request, id=None):
    return self.create(request)

    def put(self, request, id=None):
    return self.update(request, id)

    def delete(self, request, id):
    return self.destroy(request, id)

Comments are closed.

Codeloop
Share via
Copy link
Powered by Social Snap
×