简体   繁体   English

改变 ListView get_queryset? django 中的分页

[英]changeing ListView get_queryset? pagination in django

Good day everyone.今天是个好日子。 I am having some trouble with the paginator in django.我在使用 django 中的分页器时遇到了一些问题。 I have in my db all the customers info, so what I want to do is to display that info, but I made a search function in which you clic a letter button, for example you clic A and it will filter all the customers that their last name starts with the letter A, if you clic B, it will filter all customers with their last name start with B, and so on.我的数据库中有所有客户信息,所以我想做的是显示该信息,但我进行了搜索 function 在其中单击字母按钮,例如单击 A,它将过滤所有客户姓氏以字母A开头,如果单击B,它将过滤所有姓氏以B开头的客户,依此类推。 This works correctly, the problem is that I also want to display 10 customers per page, so if I have 20 customers that their last name starts with the letter A, what you will see, will be 10 customers and a bar that says ( <<< page 1 page 2 >>> ) or something like that, and that should be solved with the paginator, I added it, but its not working.这可以正常工作,问题是我还想在每页显示 10 个客户,所以如果我有 20 个客户,他们的姓氏以字母 A 开头,你会看到 10 个客户和一个显示 (< << page 1 page 2 >>> ) 或类似的东西,应该用分页器解决,我添加了它,但它不起作用。 I think the problem is that maybe my get function is rewriting the get_query function from ListView perhaps?我认为问题在于,也许我得到的 function 正在重写来自 ListView 的 get_query function? I tryed different things but I'm not sure.我尝试了不同的东西,但我不确定。 Here is my code in views:这是我在视图中的代码:

class ExpedientView(ListView):
    queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
    template_name = 'dashboard-admin/portfoliorecords.html'
    paginate_by = 10

    def get(self,request):
        if request.GET['letter'] == '':
            context_object_name = 'portfolios'
            queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
            context = queryset
        else:
            letter = request.GET['letter']
            context_object_name = 'portfolios'
            queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name').filter(owner__last_name__istartswith=letter)
            context = queryset
        return render(request, 'dashboard-admin/portfoliorecords.html', {'portfolios': context})

the get(self,request) function, works perfectly, however the first part where the paginated_by is not working. get(self,request) function 运行良好,但是 paginated_by 的第一部分不起作用。

Before I added the get function, and just filtering all the customers, the paginator worked fine, so in the template, the code works properly.在我添加 get function 并过滤所有客户之前,分页器工作正常,所以在模板中,代码工作正常。

If all you need to do is dynamically change the queryset then instead of overriding get (Which calls the appropriate functions in the view, which will perform all pagination tasks, etc.) you should be overriding get_queryset :如果您需要做的只是动态更改查询集,那么您应该重写get_queryset而不是覆盖get (它会调用视图中的相应函数,这将执行所有分页任务等):

class ExpedientView(ListView):
    queryset = Portfolio.objects.filter(products__isnull=True)
    template_name = 'dashboard-admin/portfoliorecords.html'
    paginate_by = 10
    ordering = 'owner__last_name' # Put the ordering here instead of specifying it in the queryset

    def get_queryset(self):
        queryset = super().get_queryset()
        letter = self.request.GET.get('letter')
        if letter:
            queryset = queryset.filter(owner__last_name__istartswith=letter)
        return queryset

You have to modify the def get_queryset(self) method not the def get(self, request) method您必须修改def get_queryset(self)方法而不是def get(self, request)方法

Remove the def get(self, request) method and the below code.删除def get(self, request)方法和下面的代码。

def get_queryset(self):
    queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
    letter = request.GET.get('letter', None)
    if letter:
        queryset.filter(owner__last_name__istartswith=letter)
    return queryset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM