List Info

Thread: ManyToManyField and order_by




ManyToManyField and order_by
country flaguser name
United States
2007-08-24 16:13:33
Hi,

I'm trying to do some simple ordering without having to
resort to raw
sql. Let's say I have models similar to the pizza example in
the
documentation:

class Topping(models.Model):
    # ...

class Pizza(models.Model):
    # ...
    toppings = models.ManyToManyField(Topping)

I want to get a list of Pizzas ordered by number of toppings
(i.e.
pizza with the most number of toppings at the beginning of
the list).
I see that toppings has a count() method but I can't figure
out a way
to use this in ordering.

Thanks for any help,

Viraj.


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


Re: ManyToManyField and order_by
country flaguser name
United States
2007-08-31 11:34:45
Anyone have ideas on this? I can do it with extra():

http://ww
w.djangoproject.com/documentation/db-api/#extra-select-none-
where-none-params-none-tables-none

But this is seems bad to resort to sql. This seems like such
a common
operation to do.

Thanks,

Viraj.

On Aug 24, 2:13 pm, Viraj Alankar <valan...gmail.com> wrote:
> Hi,
>
> I'm trying to do some simple ordering without having to
resort to raw
> sql. Let's say I have models similar to the pizza
example in the
> documentation:
>
> class Topping(models.Model):
>     # ...
>
> class Pizza(models.Model):
>     # ...
>     toppings = models.ManyToManyField(Topping)
>
> I want to get a list of Pizzas ordered by number of
toppings (i.e.
> pizza with the most number of toppings at the beginning
of the list).
> I see that toppings has a count() method but I can't
figure out a way
> to use this in ordering.
>
> Thanks for any help,
>
> Viraj.


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


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )