Re: Ordering results by m2m relations without multiply results

2008-09-27 Thread Alessandro
2008/9/27 Malcolm Tredinnick <[EMAIL PROTECTED]>:


> You might want to prefer to do the ordering in Python or as a second
> pass.

Is it possible to order in python a queryset object? The generic view
I use needs a queryset.
If it's not possible I have to rewrite my generic object list to use a
python ordered list.



-- 
Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net

SOASI Soc.Coop. - www.soasi.com
Sviluppo Software e Sistemi Open Source
Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

--~--~-~--~~~---~--~~
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: Ordering results by m2m relations without multiply results

2008-09-27 Thread Alessandro Ronchi
2008/9/27 Malcolm Tredinnick <[EMAIL PROTECTED]>:

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

Ok.
Is it possible to pass the queryset to the generic view after ordering
with python, or I have to rewrite also the generic view?



-- 
Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net

SOASI Soc.Coop. - www.soasi.com
Sviluppo Software e Sistemi Open Source
Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

--~--~-~--~~~---~--~~
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: Ordering results by m2m relations without multiply results

2008-09-26 Thread Malcolm Tredinnick


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



Ordering results by m2m relations without multiply results

2008-09-26 Thread Alessandro
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().

Is there a solution?

-- 
Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net

SOASI Soc.Coop. - www.soasi.com
Sviluppo Software e Sistemi Open Source
Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

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