On Sat, Oct 11, 2008 at 8:38 AM, Stephen Cheng
<[EMAIL PROTECTED]>wrote:
>
> I am wondering how to get distinct values for a ForeignKey field
>
> For example:
> class ModelA(models.Model):
> .........................
> city=models.ForeignKey(City)
>
distinct_cities = ModelA.values('city').order_by().distinct()
will return a ValuesQuerySet distinct_cities containing the distinct city
values. The empty order_by() is to avoid pulling in any default ordering
fields that may interfere with getting distinct city values, see the note
here:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
>
> I understand that I can pass the list to template than probably use
> regroup and get the group values, but is there a efficient way to
> handle this without using SQL statement?
>
You rather lost me here because I don't know what 'the list' is you are
referring to nor the 'SQL statement'. Hopefully I answered your question
above.
Karen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---