Paginate Django formset

前端 未结 5 910
失恋的感觉
失恋的感觉 2020-12-29 14:42

I have a model formset that I want to display 10 forms at a time using Django\'s Paginator, but it can\'t be done like paginator = Paginator(formset, 10). What\

5条回答
  •  有刺的猬
    2020-12-29 15:23

    Agree with Elrond Supports Monica. Fake attribute is interesting way to resolve the ordering error (Cannot reorder a query once a slice has been taken.)

    But it can be fixed in one line also queryset = queryset.order_by(Entry._meta.pk.name)

    This fake ordering is need for avoid error in django.form.modelsBaseModelFormSet(BaseFormSet).get_queryset(): line #640
    that make artificial ordering by pk but it impossible after slicing (LIMIT-ations in SQL )

    More detailed example

        queryset = Entry.objects.all()
        queryset = queryset.order_by(Entry._meta.pk.name)
    
        paginator = Paginator(object_list=queryset, per_page=10)
        page_obj = paginator.get_page(request.GET.get('page'))
    
        EntryFormSet = modelformset_factory(Entry, EntryForm, extra=0)
        entryformset = EntryFormSet(queryset=page_obj.object_list)
    

提交回复
热议问题