On Fri, 2008-09-26 at 16:29 +0200, Alessandro wrote:
> I have a model with a m2m field "provincia".
> I want to order my objects in this way:
> If the object has a provincia = 'AA' on m2m relation must be the
> bottom of my list.
> 
> I tried query = query.order_by('-provincia') and it works correctly
> (because AA is the first of the values), but I duplicates the results
> because of multiple m2m relationships:
> If an object has both "FC" and "BO" in provincia, I will have 2
> objects in queryset also If I use distinct().

That's correct, since the fields you are ordering on are part of the
selected columns, so they make the entries non-distinct.
> 
> Is there a solution?

Not really, except for writing a raw SQL query. The raw SQL for this
type of query is very complicated and it's such an edge case (it's
trying to do if-then logic in SQL when you write it out) and not always
even possible, so Django doesn't even try. If you order on a
multi-valued field and have more than one related value, you'll get
multiple rows.

You might want to prefer to do the ordering in Python or as a second
pass. Or you could try to write the raw SQL, but it could be fairly
messy.

Regards,
Malcolm


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

Reply via email to