limit row in change view

98 views
Skip to first unread message

Nenad Cikic

unread,
May 9, 2012, 12:16:44 AM5/9/12
to django...@googlegroups.com
Hello!
I am trying to undestand how to limit visible rows in change view. It seems to me a common need, but can not find info in django docs.
I am using the admin interface.
So I have one model and his admin is something as:

    ordering = ('-datum','krajnjikorisnik','tipimpulse','impulsecijena')

    def queryset(self, request):
           qs=super(KrajnjiKorisnikImpulseAdmin, self).queryset(request)
           qs=qs.filter(bla bla bla)
           return qs

With the queryset of above I filter just the needed rows. I have check with postgre log, and indeed the correct SELECT statement is generated.

Now I want to get just the top x elements.
I continue to get
       "Cannot reorder a query once a slice has been taken."
or
        "Cannot filter a query once a slice has been taken."

I have tried to change my queryset function to:
1.
    def queryset(self, request):
           qs=super(KrajnjiKorisnikImpulseAdmin, self).queryset(request)[0:2]
           qs=qs.filter(bla bla bla)
           return qs
2. i have treid to manually construct the queryset for testing purpose as:
    def queryset(self, request):
           qs=KrajnjiKorisnikImpulse.objects.all()[0:2]
           return qs
and i have also commented the ordering statement

Both are giving the above errors. Is not limiting the rows a common feature one might want, or is not relevant since admin views have pages?

Thanks
Nenad

Nenad Cikic

unread,
May 9, 2012, 11:57:56 AM5/9/12
to django...@googlegroups.com
I have managed to solve it by myself so reporting here if anyone is interested. I have checked and the sql Log shows statements are generated with LIMIT clause (postgre).
You have to subclass the ChangeList class ass
class MyChangeList(ChangeList):
    def get_query_set(self):
        return super(MyChangeList, self).get_query_set()[0:2] #here for test i am retrieving top 2 rows

then in models admin:
    def get_changelist(self, request, **kwargs):
        return MyChangeList

Bye
Nenad

Reply all
Reply to author
Forward
0 new messages