On Wed, Jul 27, 2011 at 9:29 AM, Ian Clelland <clell...@gmail.com> wrote:

>
>
> On Mon, Jul 25, 2011 at 11:29 AM, Alfredo Alessandrini <
> alfreal...@gmail.com> wrote:
>
>> Hi,
>>
>> I've a model testDB with a foreignkey "country" related to another
>> database.
>>
>> I need to retrieve the unique value of country.common_name.
>>
>> With this
>>
>> country_list = testDB.objects.values('country').distinct
>>
>> I can retrieve the unique value, but only the ID.
>>
>> In [63]: testDB.objects.values('country').distinct
>> Out[63]: <bound method ValuesQuerySet.distinct of [{'country': 0L},
>> {'country': 0L}, {'country': 0L}, {'country': 0L}, {'country': 0L},
>> {'country': 0L}, {'country': 0L}, {'country': 0L}, {'country': 0L},
>> {'country': 0L}, {'country': 0L}, {'country': 0L}, {'country': 0L},
>> {'country': 0L}, {'country': 0L}, {'country': 0L}, {'country': 0L},
>> {'country': 0L}, {'country': 0L}, {'country': 0L}, '...(remaining
>> elements truncated)...']>
>>
>> How can I  retrieve the attribute "common_name" of the country object?
>>
>
> The "<bound method ...>" is usually a clue that what you have retrieved is
> actually a method, and you haven't called that method yet.
>
> Try:
> testDB.objects.values('country').distinct()
>
> (Although you should probably also read the note here:
> https://docs.djangoproject.com/en/1.3/ref/models/querysets/#distinct to
> see why you might want to  be writing
> testDB.objects.order_by().values('country').distinct() )
>
>

And this time, I'll actually respond after reading the last line of your
question -- if what you want is a list of distinct country *names*, where
the name is a field on the country object, you are looking for somethng like
this:

testDB.objects.order_by().values('country__common_name').distinct()


-- 
Regards,
Ian Clelland
<clell...@gmail.com>

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to