On Wed, Feb 18, 2009 at 7:06 PM, Sean Brant <brant.s...@gmail.com> wrote:

>
> Is there a simple way to say get me 4 objects from model that come
> before this object and 4 that come after this object.
>
> So lets say I have a Show. I would like to know which 4 shows come
> before the current show and which 4 come after, however I must limit
> the shows based on a category type. Would slicing work for this?
>
> If this is not sure clear let me know and I will try and clarify more.
> >
>
You can do this with slicing fairly easily, basically find whatever field
you want to order by and do something like:
Model.objects.filter(field__gt=obj.field).order_by('field')[:4] to get the 4
greater than it and
Model.objects.filter(field__lt=obj.field).order_by('-field')[:4] to get the
4 less than it.

Hope this helps,
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

--~--~---------~--~----~------------~-------~--~----~
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