On Aug 13, 12:06 am, cerberos <pe...@whywouldwe.com> wrote:
> On Aug 12, 11:52 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
>
>
>
> > On Wed, Aug 12, 2009 at 11:49 AM, cerberos<pe...@whywouldwe.com> wrote:
>
> > > Say there are 10 records, I want records 3 & 4 in descending order.
>
> > > q1 = ModelName.objects.order_by('id').filter(id__gte=3)[:2] # gives
> > > the records I want but in ascending order
> > > q2 = ModelName.objects.order_by('-id').filter(id__gte=3)[:2] # gives
> > > the last 2 records (9 & 10) in correct order
> > > q3 = ModelName.objects.order_by('id').filter(id__gte=3)[:2].order_by('-
> > > id') # Assertion Error: cannot reorder query once a slice has been
> > > taken
> > > q4 = ModelName.objects.order_by('id').filter(id__gte=3)[:2].reverse()
> > > # Same as q2
>
> > > So can I get the correct records in the wrong order or incorrect
> > > records in the right order. I think I need to use extra() but don't
> > > know how exactly (I have read the docs and had a few attempts but the
> > > results were as above).
>
> > Your best bet would be to take the items you want.  And do
>
> > items = list(items)
> > items.reverse()
>
> > to put them in the correct order in Python, this is going to be easier
> > than mucking around with custom SQL.
>
> > Alex
>
> > --
> > "I disapprove of what you say, but I will defend to the death your
> > right to say it." -- Voltaire
> > "The people's good is the highest law." -- Cicero
> > "Code can always be simpler than you think, but never as simple as you
> > want" -- Me
>
> Thanks, I realised that converting to a list would work just after I
> posted.
>
> That led me to a solution. For some reason I was trying id_gte=3, with
> id_lte=4 this solution works.
>
> ModelName.objects.order_by('id').filter(id__lte=4)[:2]


above should be order_by('-id')

I should add that the solution I'm using works for my situation but it
doesn't solve the originally posted problem - I'm now specifying
record 4 instead of 3.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to