Django Rest Framework
* REST stand for Representational State transfer
* Rest consumer is our web browser and Rest provider is our website
* So we can connect to the rest provider with the rest consumer with HTTP protocol and in almost all the website we can perform the CRUD operation through post, read, patch,delete methods
* Our restful provider should support multiple formats and HTTP make it very easy
* Restful services offers interoperable meaning any application running on any platform written in any language can be consumed by any other application running on completely different platform written in different language and that through multiple data format (JSON,XML,CSV)
* Django Rest Framework
* Django rest framework is extension of django framework that make it super easy for developers to create and maintain restful api
* We can create rest API through class based views as well as function based views
* DRF offer Serializers
* We get the request form different websites n the form of XML, JSON,CSV so what serializer does is it convert these format into python object and save it to the database
* so these Serializer do Serialization as well as Deserialization
* Serialization is process of converting python object into xml/json/csv aur deserialization ulta hai
* We can save the data to the database through orm
* ORM is without writing a single line of SQL you can create a database through django model
* Web Browserable API : if you want to test your application or browse through your restful API that you have created , django provide beautiful user interface so when you run your application and when you access your restful API you need not any testing tool , instead that django will give you a UI , here you can perform all the HTTP operation like get,put ,post,delete
*Security: every restful application or web application should be secure so we need Authentication, Authorization, and all these support is built in django
* to django to communicate with MySQL us pip install mysqlclient
* Postman is restful client we use this to test our microservices restful api
* you can create the view using function based views as well as class based views , in class based views the numbers of line of code get reduced
* so whatever conditional logic you have in function based views, that will go to the individual methods
* Mixin uses dry principle, by using mixins the number of line of code get reduced
* Generic View: Using mixin classes we have significantly reduces the number of line of code we have to write to create our restful API, but still we have to repeat the handler methods and action methods across the restful API we created that is where django rest framework generics comes in and reduces the lines of code
here in generic view you have combination of classes like listCreateapiview which is for non primary key based operation, and we have RetereiveUpdateDestroyAPI view which support all primary key based operation
* Viewsets: Using mixins and generic classes we have significantly reduces the number of line of codes that we have to write to create our restful API , but one issue we still have is we have to create two different classes to support non-primary key based operation and primary key based operations and we have to configure the URL for both that is where DRF viewsets comes to action
# viewsets are set of views that we create either by extending a viewset class or by extending a Model viewset class
# if you extend the viewset then you have to provide the implementation for list(), create(), reterive(), update(), delete()
But if you extend Model viewset then you need not write all these methods this will comes automatically
All you need to do is configure the router for model viewset
* We use nested serializer because if there is any author serializer and we add another serializer called book serializer so when author information is serialized all his book information data should also sent back to the client
* Pagination:
* When data is sent back from restful provider to restful client, if al the database record sent back at once , then it is too much for client to handle , that is where pagination comes in
the data is split across multiple pages, each page have certain number of records , based on what we configure
Client can deal with one page of record at a time
Pagination provide link to client of each page using which you can navigate to the next page or previous page , these links are usually communicated through either HTTP header or response itself directly in json resoponse
Thanks to Django rest framework it gives us pagination classes which are ready to use instead fo we coding pagination from scratch in our restful endpoint we can directly use the inbuilt class available in setting.py you can configure the default pagination classes
if PAGE_SIZE =2 ,
it means in each page there will only be two records
these pagination works for generics and viewsets only
There are different type of paginations
PageNumgerPagination - page
LimitOffsetPagination - limit means no. of page and offset gives the index of page
* Authentication is process of making sure that particular user is allowed to access our application or not
* Authorization is like a giving power to the other user , but you can restrict it
* We can make all the views need to be authorized by doing some settings in setting.py
and that setting is this
REST_FRAMEWORK={
'DEFAULT_AUTHENTICATION_CLASSES':['rest_framework.authentication.BasicAuthentication'],
'DEFAULT_PERMISSION_CLASSES':['rest_framework.permissions.IsAuthenticated','rest_framework.permissions.DjangoModelPermissions']
}
Comments
Post a Comment