get_next_by title?

176 Aufrufe
Direkt zur ersten ungelesenen Nachricht

ChrisR

ungelesen,
16.04.2010, 17:36:4016.04.10
an Django users
I know of the get_next_by_FOO for getting the next object by date, but
I need a way to get the next object by a title or name on a
object_detail page.

I've been searching around and can't find anything to help.

Any thoughts or suggestions?

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

ChrisR

ungelesen,
19.04.2010, 00:55:1319.04.10
an Django users
I still haven't found anything useful to help on this.

Anyone have any thoughts, suggestions, pointers?

Daniel Roseman

ungelesen,
19.04.2010, 07:24:4619.04.10
an Django users
On Apr 19, 5:55 am, ChrisR <robthech...@gmail.com> wrote:
> I still haven't found anything useful to help on this.
>
> Anyone have any thoughts, suggestions, pointers?
>

The basic algorithm for get_next_by_FOO is just:
MyModel.objects.order_by('FOO').filter(FOO__gt=myobject.FOO)[0]
In other words, order by the relevant field, then get the first object
whose value for that field is greater than the value in the existing
model. That's quite easy to replicate in your own code.
--
DR.

ChrisR

ungelesen,
23.04.2010, 02:29:3823.04.10
an Django users
Thanks Daniel!

I didn't realize that "FOO_gt=..." would go to the next. I guess I
was thinking that'd only work with dates, ints, things that have to do
with numbers. (I'm new to this stuff).

Provided what you gave me, I came up with the follow:

def get_prev_by_title(self):
get_prev = Product.objects.order_by('-
title').filter(title__lt=self.title)
try:
return get_prev[0]
except IndexError:
return None


def get_next_by_title(self):
get_next =
Product.objects.order_by('title').filter(title__gt=self.title)
try:
return get_next[0]
except IndexError:
return None

Javier Guerra Giraldez

ungelesen,
23.04.2010, 12:35:2323.04.10
an django...@googlegroups.com
On Fri, Apr 23, 2010 at 1:29 AM, ChrisR <robth...@gmail.com> wrote:
>        def get_prev_by_title(self):
>                get_prev = Product.objects.order_by('-
> title').filter(title__lt=self.title)
>                try:
>                        return get_prev[0]
>                except IndexError:
>                        return None
>
>
>        def get_next_by_title(self):
>                get_next =
> Product.objects.order_by('title').filter(title__gt=self.title)
>                try:
>                        return get_next[0]
>                except IndexError:
>                        return None

just a little DRYer:

====== take 1 ========
def firstornone (q):
try:
return q[0]
except IndexError:
return None

.............

def get_prev_by_title(self):
return
firstornone(Product.objects.order_by('-title').filter(title__lt=self.title))

def get_next_by_title(self):
return
firstornone(Product.objects.order_by('title').filter(title__gt=self.title))

====================
or even:
======== take 2 =======
def next_by (queryset, field, value):
return firstornone
(queryset.order_by(field).filter(**{'%s__gt'%field:value}))
def prev_by (queryset, field, value):
return firstornone
(queryset.order_by('-'+field).filter(**{'%s__lt'%field:value}))

.....
def get_prev_by_title(self):
return prev_by(Product.objects.all(), 'title', self.title)

def get_next_by_title(self):
return next_by(Product.objects.all(), 'title', self.title)

=================

(a slow morning, i felt the itch to code a little....)

--
Javier

ChrisR

ungelesen,
23.04.2010, 13:29:2523.04.10
an Django users
Awesome, even better!

Thanks Javier. I'll look into adding this instead, or at least hang
onto it for future reference.
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten