1

I need to paginate my modelformset_factory but I am getting this exception:

Django Version: 1.6.2
Exception Type: AttributeError
Exception Value:    
'Page' object has no attribute 'ordered'

I have this in my view:

BrandFormSet = modelformset_factory(Brand, form=BrandFormList, extra=0, **kw)

paginator, brands = paginate(request, Brand.objects.filter(shop=shop), 10)

paginate function code:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def paginate(request, object_list, per_page):
    paginator = Paginator(object_list, per_page)

    page = request.GET.get('page')
    try:
        objects = paginator.page(page)
    except PageNotAnInteger:
        objects = paginator.page(1)
    except EmptyPage:
        objects = paginator.page(paginator.num_pages)

    return paginator, objects

Now this in my view:

print type(Brand.objects.filter(shop=request.user.shop))
print type(brands)

Outputs:

<class 'django.db.models.query.QuerySet'>
<class 'django.core.paginator.Page'>

I am not sure what to do at this point.

halfer
  • 19,824
  • 17
  • 99
  • 186
Adam
  • 2,948
  • 10
  • 43
  • 74

1 Answers1

2

The problem here is that you're using brands (a Page) in a context that's expecting a QuerySet.

It doesn't seem as though pagination and formsets were designed to work together. From glancing at the source code, it looks like brands.object_list might still be a QuerySet. If so, use that where you were using brands before.

This answer gives a hacky solution for using the two together, but it's quite inefficient. (It makes one trip to the database to get the desired item ids, and then constructs a new QueryList that explicitly includes those ids.)

Community
  • 1
  • 1
Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
  • With brands.object_list I got a new error: Exception Value: Cannot reorder a query once a slice has been taken. – Adam Mar 16 '14 at 15:32
  • @Adam: Hmm, that strengthens my feeling that these two features were just not designed to work together. I don't have a better idea than the one in the answer I linked to, where he just creates a new queryset from scratch... – Kevin Christopher Henry Mar 17 '14 at 05:40