首页 > 解决方案 > Django ListView 派生类忽略 paginate_by 字段

问题描述

我正在使用 Django 3.2

我正在使用 OOP 来保持我的代码干燥。我有一个基类 Foo(它派生自 ListView),并且我在该类中设置公共属性。

然后我有两个 Foo 的特化——我希望知道paginate_by成员变量——但是,子类似乎忽略了我设置的变量paginate_by,因为它没有效果。

这是我的代码:

class UserFooList(LoginRequiredMixin, ListView):
    http_method_names = ['post']
    paginate_by = 2


    def post(self, request, *args, **kwargs):

        # some processing logic

        return render(request, '/path/to/Foo_list.html', context=context, status=status_code)


    
class UserApprovedFooList(UserFooList):
    
    def get_queryset(self):
        return Foo.objects.prefetch_related().filter(/* query criteria one */)

 

class UserFoosInModerationQueueList(UserFooList):
    
    def get_queryset(self):
        return Foo.objects.prefetch_related().filter(/* query criteria two */)

为什么该paginate_by字段被UserApprovedFooListand UserFoosInModerationQueueList- 忽略,我该如何解决?

标签: djangodjango-viewsdjango-class-based-views

解决方案


推荐阅读