django 分页出现 UnorderedObjectListWarning 错误

时间:2023-03-09 18:51:22
django 分页出现 UnorderedObjectListWarning 错误

django 分页出现此错误:

UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'monitor.models.HostBind'> QuerySet.
allow_empty_first_page=allow_empty_first_page, **kwargs)

原因为:

view代码中没有进行排序指定,

class MonitorListView(PaginateListView):
model = Monitors
template_name = 'monitors_list.html'
context_object_name = 'monitor_list'
page_kwarg = 'page'
ordering = 'name' ##加入ordering排序

或者使用

class MonitorListView(PaginateListView):
queryset = Monitors.objects.all().order_by('name')
template_name = 'monitors_list.html'
context_object_name = 'monitor_list'
page_kwarg = 'page'