Reasons for inheriting GenericAPIView
Previously, APIview inherited from the most basic view of the rest framework. Now we upgrade and inherit GenericAPIView.
GenericAPIView It's inheritance APIView Yes, use fully compatible APIView,It mainly adds the methods of operating serializer and database query, The function is to Mixin Provides method support for the execution of extension classes. Usually, when in use, it can cooperate with one or more Mixin Extension class a key: GenericAPIView stay APIView What has been accomplished on the basis of 1)get_queryset(): Slave class properties queryset Obtained in model of queryset data Group operation just go get_queryset()method(Including group search, group increase, etc) 2)get_object(): Slave class properties queryset Obtained in model of queryset data Then group by name pk Determine the unique operation object Just operate and go get_object()Methods (including single query, single addition, etc.) 3)get_serializer(): Slave class properties serializer_class Obtained in serializer Serialization class for
That is, it encapsulates and inherits some methods in APIview.
Properties and methods used by serializers provided after inheriting GenericAPIView
About serializer properties
The names of the following two attributes cannot be changed. They are attributes in the GenericAPIView class
queryset indicates the data required by the view (model query data)
serializer_class indicates the serializer used by the view
for instance:
"""List view""" # Specify serializer class serializer_class = BookInfoModelSerializer # Specify query set 'data source' queryset = BookInfo.objects.all()
Properties and methods of database query provided by
1.get_queryset() obtains the queryset data of the model from the class attribute queryset and queries multiple data
2.get_object() obtains the queryset data of the model from the class attribute queryset, and then determines the unique operation object through the famous group pk. That is to obtain detailed data
3.get_serializer() serializer from class attribute_ Class to obtain the serializer's serialization class, which is mainly used to provide it to the Mixin extension class. Get serializer object
Usage of detail page view: (that is, in the method in detail view, we can also define the following two properties)
lookup_field query of user-defined primary key name group. The default is' pk '
Lookup_ url_ When kwarg queries a single data, the parameter keyword name in the URL is the same as look by default_ Same as field
Inherit GenericAPIView to realize all queries
"""The following is inheritance GenericAPIView View of""" class BookListGenericView(GenericAPIView): """List view""" # Specify serializer class serializer_class = BookInfoModelSerializer # Specify query set 'data source' queryset = BookInfo.objects.all() def get(self, request): qs = self.get_queryset() # Get dataset serializer = self.get_serializer(qs, many=True) # Get serializer object return Response(serializer.data) # Take out the data from the serializer object and return it
Inherit GenericAPIView to query one, that is, details
Usage of detail page view: (that is, in the method in detail view, we can also define the following two properties)
lookup_field query of user-defined primary key name group. The default is' pk '
Lookup_ url_ When kwarg queries a single data, the parameter keyword name in the URL is the same as look by default_ Same as field
That is, the above two properties are used in details
class BookDetailGenericView(GenericAPIView): """Detail view""" # Specify serializer class serializer_class = BookInfoModelSerializer # Specify query set 'data source' queryset = BookInfo.objects.all() def get(self, request, pk): book = self.get_object() #get_ The object () method finds the data object in the queryset according to the pk parameter serializer = self.get_serializer(book) return Response(serializer.data)
Inherit GenericAPIView to modify data
class BookDetailGenericView(GenericAPIView): """Detail view""" # Specify serializer class serializer_class = BookInfoModelSerializer # Specify query set 'data source' queryset = BookInfo.objects.all() def put(self, request, pk): book = self.get_object() # Query the data of the specified pk serializer = self.get_serializer(book, request.data) # Get serializer object serializer.is_valid(raise_exception=True) # Verification successful serializer.save() # to update return Response(serializer.data) # Return updated data
route
# # Routing GenericAPIView for list view url(r'^books/$', views.BookListGenericView.as_view()), # Routing GenericAPIView for detail view url(r'^books/(?P<pk>\d+)/$', views.BookDetailGenericView.as_view()),