Python test development django-rest-framework-87. Pagination query offset pagination (LimitOffsetPagination) and cursor pagination (CursorPagination)…

Foreword The django-rest-framework paginator provides 3 paging methods. The previous article introduced simple paging (PageNumberPagination). This article continues to introduce the other two paging offset paging (LimitOffsetPagination) and cursor paging (CursorPagination) Offset Pagination (LimitOffsetPagination) LimitOffsetPagination is offset paging. When querying, the url address carries two parameters, limit and offset, in the following format http://localhost:8000/api/v1/goods?limit=100 & […]

python test development django-rest-framework-93. UniqueTogetherValidator joint unique verification of deserialization (ModelSerializer)…

Foreword When adding a product earlier, the product code can only be added once and can be verified with a unique field using the UniqueValidator. If a user collects a product, one user can collect multiple products, and one product can also be collected by multiple people. However, the same person can only collect the […]

Python test development django-rest-framework-91. Deserialization (ModelSerializer) ChoiceField option field verification…

Foreword When we need to verify the option field, we need to use ChoiceField to verify options There is a field in the model model that is an option field. goods_status can have two statuses, 0 means off the shelf, 1 means on sale, the default class Goods(models.Model): “””Product list””” goods_status = models.IntegerField(choices=( (0, ‘Removed’), […]

Python test development django-rest-framework-88. Deserialization (ModelSerializer) verification of incoming parameters

Foreword serializers.Serializer can serialize fields in the model model, and two methods, create and update, must be written. ModelSerializer can be regarded as an upgraded version of Serializer, which is more powerful and convenient. In fact, the ModelSerializer class inherits the Serializer class Serialization Serialization is to convert the data in the database into json […]

Python test development django-rest-framework-63. Function-based view (@api_view())

Foreword The previous article talked about class-based views. In the REST framework, you can also use regular function-based views. It provides a simple set of decorators to wrap your view functions, This ensures that the view function receives a Request (rather than Django’s general HttpRequest) object and returns a Response (rather than Django’s HttpResponse) object, […]

python test development django-rest-framework-62. Class-based views (APIView and View)

Foreword There are two ways to edit views.py in Django. One is a class-based implementation, and the other is a functional implementation. Both methods can be used. The REST framework provides an APIView class, which is a subclass of the Django View class. The difference between View and APIView View is Django’s default view base […]

python test development django-rest-framework-64. serialization (serializers.Serializer)

Foreword The serializers in the REST framework are very similar to Django’s Form and ModelForm classes. We provide a Serializer class that provides you with powerful general methods to control the output of the response, and a ModelSerializer class that provides a useful shortcut for creating serializers for working with model instances and querysets. serializers.Serializer […]

Python test development django-rest-framework-65. Serialization (ModelSerializer)

Foreword serializers.Serializer can serialize fields in the model model, and two methods, create and update, must be written. ModelSerializer can be regarded as an upgraded version of Serializer, which is more powerful and convenient. In fact, the ModelSerializer class inherits the Serializer class. ModelSerializer The ModelSerializer class allows you to automatically create a Serializer class […]

Django-rest-framework framework filtering, sorting, paging exception handling

一Filtering For list data that may need to be filtered based on fields, we can enhance support by adding the django-fitlter extension. pip install django-filter Add filtering backend settings in the configuration file: INSTALLED_APPS = [ … ‘django_filters’, # Need to register the application, ] REST_FRAMEWORK = { … ‘DEFAULT_FILTER_BACKENDS’: (‘django_filters.rest_framework.DjangoFilterBackend’,) } Add the filter_fields […]