Re: Casting a raw query set as a list for pagination

627 views
Skip to first unread message

Bill Freeman

unread,
Mar 26, 2013, 2:33:19 PM3/26/13
to django...@googlegroups.com
Wouldn't you be better served by implementing the count() (which is probably what the paginator calls) method on your raw queryset (subclassing as necessary, whatever a raw queryset is), rather than poking into paginator internals?

On Tue, Mar 26, 2013 at 5:51 AM, <chambe...@gmail.com> wrote:

I'm trying to implement Django's built in pagination feature with a raw query set. I've researched the issue and the answer is I need to cast my set as a list. Something like this:

paginator = Paginator(refg, 100) # Show 100 contacts per page
paginator._count = len(list(refg))

From my understanding, setting the count field for the paginator should prevent Django from trying to get the size of my raw set, but this is not the case. I'm still getting the "RawQuerySet has no len() operation" error. I've attched the relevant code from my views.py and my template.

Can I get some advice as to how I can fix this error?


views.py

refg = RefGene.objects.raw(qrefg)

paginator = Paginator(refg, 100) # Show 100 contacts per page
paginator._count = len(list(refg))

# using django's generated forms
c = RequestContext(request, {
    "refg": refg, ... })

return HttpResponse(t.render(c))

template

{% autopaginate refg %} {% for r in refg %} <tr> <td>{{ r.data}}</td> <td>{{ r.loc}}</td> </tr> {% endfor %} {% paginate %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matt Buck

unread,
Apr 26, 2014, 4:12:18 PM4/26/14
to django...@googlegroups.com
I've created a paginator for RawQuerySets.  https://github.com/seamusmb/django-paginator-rawqueryset
Reply all
Reply to author
Forward
0 new messages