Re: database api question (query/filter related objects)

2007-11-04 Thread Karen Tracey
On 11/4/07, Brot <[EMAIL PROTECTED]> wrote:
>
> Ok, thank you... that's standard python stuff. It's no secret, that
> I'm new to python :-(
> But I found another solution. I don't really now which on is the
> better one?!
>
> partnerlist = [ep.partner for ep in
> Event.objects.latest
> ('date').eventpartner_set.all().select_related().order_by('hp_partner.name')]


Ah yes, that'll work too, it just didn't occur to me.  I suspect it might
also be more efficient, though that's just a gut feeling.  You could run
some little tests with timeit (http://docs.python.org/lib/module-timeit.html)
to find out.  Not that more efficient is necessarily best, unless this is a
critical code path where you're expecting to be sorting good-sized lists.
Readability/maintainability counts also, and you're the best judge of what's
most readable/maintainable for your code.

You're not alone in being new to Python with Django, that seems more common
than not around here (and was the case with me too).  But I've found Python
to be a powerful tool and well worth learning in some depth (though I'm
hardly an expert) in addition to Django.

Karen

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: database api question (query/filter related objects)

2007-11-04 Thread Brot



On Nov 4, 7:02 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> On 11/4/07, Brot <[EMAIL PROTECTED]> wrote:
>
>
>
> > > partner_list = [ep.partner for ep in Event.objects.latest
> > > ('date').eventpartner_set.all()]
>
> > Ok, that works! But there is another problem now. I would like to sort
> > the list of partners.
> > The sort-criteria should be a field from the partner model. e.g: name
> > Is this possible?
>
> Sure, check outhttp://wiki.python.org/moin/HowTo/Sortingfor lots of
> examples on how to sort.  I think either
>
> partner_list.sort(lambda x,y: cmp(x.name, y.name))
>
> or (if using Python 2.4 or higher)
>
> import operator
> partner_list.sort(key=operator.attrgetter('name'))
>
> would do what you're looking for.
>
> Karen

Ok, thank you... that's standard python stuff. It's no secret, that
I'm new to python :-(
But I found another solution. I don't really now which on is the
better one?!

partnerlist = [ep.partner for ep in
Event.objects.latest('date').eventpartner_set.all().select_related().order_by('hp_partner.name')]


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: database api question (query/filter related objects)

2007-11-04 Thread Karen Tracey
On 11/4/07, Brot <[EMAIL PROTECTED]> wrote:
>
> >
> > partner_list = [ep.partner for ep in Event.objects.latest
> > ('date').eventpartner_set.all()]
>
> Ok, that works! But there is another problem now. I would like to sort
> the list of partners.
> The sort-criteria should be a field from the partner model. e.g: name
> Is this possible?
>

Sure, check out http://wiki.python.org/moin/HowTo/Sorting for lots of
examples on how to sort.  I think either

partner_list.sort(lambda x,y: cmp(x.name, y.name))

or (if using Python 2.4 or higher)

import operator
partner_list.sort(key=operator.attrgetter('name'))

would do what you're looking for.

Karen

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: database api question (query/filter related objects)

2007-11-04 Thread Brot



On Nov 4, 4:14 pm, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> On 11/4/07, Brot <[EMAIL PROTECTED]> wrote:[snip]
>
> To get the last event I use:
>
> > Event.objects.latest('date')
>
> > To get a list of EventPartners i use:
> > Event.objects.latest('date').eventpartner_set.all()
>
> > But how do I get a list of my Partners? I tried a few things, but I
> > didn't find a solution. I need the Partner-list, because I want to use
> > a template, which expect a Partner-list. It would also be possible to
> > write another template for a EventPartner-list, but that's a bad
> > solution.
>
> To extract a list of partners from a list of EventPartners, use a Python
> list comprehension:
>
> partner_list = [ep.partner for ep in Event.objects.latest
> ('date').eventpartner_set.all()]
>
> Karen

Ok, that works! But there is another problem now. I would like to sort
the list of partners.
The sort-criteria should be a field from the partner model. e.g: name
Is this possible?

Bernd


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: database api question (query/filter related objects)

2007-11-04 Thread Karen Tracey
On 11/4/07, Brot <[EMAIL PROTECTED]> wrote:[snip]

To get the last event I use:
> Event.objects.latest('date')
>
> To get a list of EventPartners i use:
> Event.objects.latest('date').eventpartner_set.all()
>
> But how do I get a list of my Partners? I tried a few things, but I
> didn't find a solution. I need the Partner-list, because I want to use
> a template, which expect a Partner-list. It would also be possible to
> write another template for a EventPartner-list, but that's a bad
> solution.
>

To extract a list of partners from a list of EventPartners, use a Python
list comprehension:

partner_list = [ep.partner for ep in Event.objects.latest
('date').eventpartner_set.all()]

Karen

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---