Using viewsets
views.py
from rest_framework import viewsets from rest_framework import mixins from rest_framework.pagination import PageNumberPagination class MyPagination(PageNumberPagination): """分页""" page_size = 10 page_size_query_param = "size" max_page_size = 50 page_query_param = "page" class DrugBidViewSets(viewsets.GenericViewSet,mixins.ListModelMixin): queryset = DrugBidModel.objects.all().order_by("id") # queryset = DrugBidModel.objects.all() serializer_class = DrugBidSerializers pagination_class = MyPagination
问题描述:UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list
这个是因为django,查询出来多条记录时,默认是无序排列的,所以 每查询一次 分页的 list 都是不一样的,因此加一个排序
Using Router
urls.py
from rest_framework.routers import DefaultRouter from drug import views router = DefaultRouter() router.register(r"drugbid",views.DrugBidViewSets) urlpatterns = [ path(‘‘, include(router.urls)), ]
原文地址:https://www.cnblogs.com/zenan/p/10516138.html
时间: 2024-10-29 09:57:01