Django: Paginator error: object of type 'User' has no len()
Django: Paginator error: object of type 'User' has no len()
我正在学习使用分页器,因为我的列表越来越长了!由于出现错误,我在实施时遇到了麻烦。我注意到如果我删除 paginate_by = 10
那么错误就会消失,但我很确定这是分页所必需的。为什么 get_queryset 与分页器冲突。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_context_data(self, *args, **kwargs):
context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
qs = self.get_queryset().notifications.all()
paginator = Paginator(qs , self.paginate_by)
page = self.request.GET.get('page')
try:
notification_pages = paginator.page(page)
except PageNotAnInteger:
notification_pages = paginator.page(1)
except EmptyPage:
notification_pages = paginator.page(paginator.num_pages)
context['notifications'] = notification_pages
return context
def get_queryset(self, *args, **kwargs):
request = self.request
return User.objects.get(pk=self.request.user.pk)
回溯:
File "myapp\lib\site-packages\django\core\paginator.py" in count
85. return self.object_list.count()
During handling of the above exception ('User' object has no attribute 'count'), another exception occurred:
File "myapp\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get
157. context = self.get_context_data()
File "myapp\src\notices\views.py" in get_context_data
18. context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get_context_data
119. paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
File "myapp\lib\site-packages\django\views\generic\list.py" in paginate_queryset
69. page = paginator.page(page_number)
File "myapp\lib\site-packages\django\core\paginator.py" in page
65. number = self.validate_number(number)
File "myapp\lib\site-packages\django\core\paginator.py" in validate_number
43. if number > self.num_pages:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in num_pages
95. if self.count == 0 and not self.allow_empty_first_page:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in count
90. return len(self.object_list)
Exception Type: TypeError at /notices/
Exception Value: object of type 'User' has no len()
get_queryset
应该 return 一个 queryset
而不是 object
.
还有一个名为 get_object
的方法,它 return 是一个对象。
您想显示特定用户的通知列表,对吗?然后你应该 return 来自 get_queryset
方法的通知列表。
我认为你应该这样做,从我的角度来看,你不应该自己处理分页,django/Django-restframework 应该在内部处理它。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
return self.request.user.notifications.all()
使用 Django Rest 框架,你只需要在你的设置文件中添加默认分页器,就像这样。
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}
或者,如果您想使用现有代码,那么您可以尝试这样的操作:
def listing(request):
notifications = request.user.notifications.all()
paginator = Paginator(notifications, 20) # Show 20 notifications per page
page = request.GET.get('page')
try:
typesets = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
typesets = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
typesets = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'typesets': typesets})
我正在学习使用分页器,因为我的列表越来越长了!由于出现错误,我在实施时遇到了麻烦。我注意到如果我删除 paginate_by = 10
那么错误就会消失,但我很确定这是分页所必需的。为什么 get_queryset 与分页器冲突。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_context_data(self, *args, **kwargs):
context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
qs = self.get_queryset().notifications.all()
paginator = Paginator(qs , self.paginate_by)
page = self.request.GET.get('page')
try:
notification_pages = paginator.page(page)
except PageNotAnInteger:
notification_pages = paginator.page(1)
except EmptyPage:
notification_pages = paginator.page(paginator.num_pages)
context['notifications'] = notification_pages
return context
def get_queryset(self, *args, **kwargs):
request = self.request
return User.objects.get(pk=self.request.user.pk)
回溯:
File "myapp\lib\site-packages\django\core\paginator.py" in count
85. return self.object_list.count()
During handling of the above exception ('User' object has no attribute 'count'), another exception occurred:
File "myapp\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get
157. context = self.get_context_data()
File "myapp\src\notices\views.py" in get_context_data
18. context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get_context_data
119. paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
File "myapp\lib\site-packages\django\views\generic\list.py" in paginate_queryset
69. page = paginator.page(page_number)
File "myapp\lib\site-packages\django\core\paginator.py" in page
65. number = self.validate_number(number)
File "myapp\lib\site-packages\django\core\paginator.py" in validate_number
43. if number > self.num_pages:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in num_pages
95. if self.count == 0 and not self.allow_empty_first_page:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in count
90. return len(self.object_list)
Exception Type: TypeError at /notices/
Exception Value: object of type 'User' has no len()
get_queryset
应该 return 一个 queryset
而不是 object
.
还有一个名为 get_object
的方法,它 return 是一个对象。
您想显示特定用户的通知列表,对吗?然后你应该 return 来自 get_queryset
方法的通知列表。
我认为你应该这样做,从我的角度来看,你不应该自己处理分页,django/Django-restframework 应该在内部处理它。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
return self.request.user.notifications.all()
使用 Django Rest 框架,你只需要在你的设置文件中添加默认分页器,就像这样。
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}
或者,如果您想使用现有代码,那么您可以尝试这样的操作:
def listing(request):
notifications = request.user.notifications.all()
paginator = Paginator(notifications, 20) # Show 20 notifications per page
page = request.GET.get('page')
try:
typesets = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
typesets = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
typesets = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'typesets': typesets})