Django-rest-framework quick start,
The first contact with django-rest-framework was during the internship. I didn't understand it at the time, and it was awesome to see that the view was written using the class method. Because the
Pagination_class=P2 Third class: Automatically help us to generate four kinds of url,index/$,index/(? p)$ fromRest_framework.routersImportDefaultrouter Router=Defaultrouter () router.register ('Index', views. Indexviewset) Urlpatterns=[url (r'^', include (Router.urls)),]classIndexviewset (viewsets. Modelviewset): Queryset=models. UserInfo.objects.all () Serializer_class=Indexserializer Pagination_class=P2classIndexserializer (serializers. Modelserializer):classMeta:model=models. UserInf
fromApi.serializers.indexImportIndexserializerclassP2 (pagenumberpagination):#number of data bars displayed per pageMax_page_size = 5page_size= 2Page_size_query_param='size' #Page NumberPage_query_param ='page' classIndexview (viewsets. Modelviewset):#you can also not inherit Modelviewset, inherit a few of the above classes, there are only a few methodsQueryset =models. UserInfo.objects.all () Serializer_class=Indexserializer Pagination_class=P2
1. Previously Django-made paging components, when the amount of data is particularly large, performance is not very high, there are three ways to handle:A. Record the last data ID of the current access page, and fetch the number of entriesB. Display up to 120 pagesC. Encrypt page numbers (show previous page, next page only)2.rest Framework PagingFrom rest_framewo
,'_data'):#调用_parse method Self._data, Self._files=Self._parse ()def_parse (self):#调用 the Select_parser method of the Defaultcontentnegotiation class, see below Parser= Self.negotiator.select_parser (self, self.parsers)#self.parser = configured object list for resolved classes at the time of package request #self.negotiator = self._default_negotiator () = Api_settings. Default_content_negotiation_class () if notparser:#if the return none description does not match, throws
Django rest framework basic introduction and sample code, djangoframework
This article focuses on Django rest framework and shares example as follows.
The Django
Source code Anatomy of the Django REST Framework authentication method
By the Django CBV mode flow, you can know the url匹配完成后,会执行自定义的类中的as_view方法 .
If there is no definition in the custom class as_view方法 , depending on the inheritance of classes in the object-oriented class, you can会执行其父类View中的as_view方法
在
Django rest framework requests and responses (detailed description), djangoframework
In the previous article, the specified data is returned when a specified URL is accessed, which also reflects the RESTful API concept. Each URL represents a resource. Of course, we also know that another feature of RESTful APIS is that different request actions will return differ
Serialization of django-rest-framework,
Preface:I learned about rest-framework serialization yesterday, so I wrote a blog record. Official Website: http://www.django-rest-framework.org/tutorial/1-serialization/?working-with-serial
The content of this section
Django Rest Framework Installation
Django Rest framwwork Environment configuration
Simple example Description
Using the rest framework i
Use Serializermethodfield in the Django REST framework to optimize unnecessary queriesFirst look at an example, in general, for objects that have parent-child relationships, we use the following method to create the class.A article class in which a article object can have multiple comment instances, the class in Django
I. Anomalies
When using the Django Rest framework, if an exception occurs, it is often shown as follows:
{"Detail": "Not allowed."}
But the backstage often wants the common pattern:
{
"desc": "Not allowed.",
"code": +,
"data": null
}
The official website document is very clear, we need to customize exception handling, and then configure it, suc
1. SettingsWe created the Django Project tutorial, and the app QuickStart: # Create a new Django Project Django-admin. # Use Virtualenvwrapper to create virtualenv# install Django and Django REST
]
The global settings configuration is as follows:
# Global rest_framework = {"default_permission_classes": ['api. utils. Permission. svippremission'],}
Three built-in Permissions
Django-rest-framework built-in permission basepermission
By default, there is no restriction on permissions.
class BasePermission(object): """ A base class from which all
settings:REST_FRAMEWORK = { ‘PAGE_SIZE‘: 10}Note that the settings in the REST framework are named a single dictionary setting called "Rest_framework", which helps to keep a good separation from your other project settings.We can also customize the paging style if we also need to, but in this case we will stick to the default.Browse APIIf we open the browser and navigate to the Browsable API, you can learn
Simple use of Django REST framework, djangoframework
See https://github.com/linux-wang/DRF_tutorial/blob/master/README.md for details
DRF has a serializer concept. The function is to convert various Django Queryset and model instances to Python native formats, saving the type problems you face when writing APIs, you c
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.