On Fri, Oct 2, 2009 at 11:49 AM, Jumpfroggy <rocketmonk...@gmail.com> wrote:

>
> I have a model with a field, and I wanted to pull all the unique
> values of that field.  So I do this:
>
>    distinct_values = Model.objects.values('some_column').distinct()
>    print 'distinct_values.count()', distinct_values.count()
>    print 'len(distinct_values)', len(distinct_values)
>
> Which gives:
>
>    distinct_values.count()    4
>    len(distinct_values)       137
>
>
I suspect you have an ordering specified for the Model that is pulling in
more columns when the query set is evaluated.  (I do not know whether it is
a bug that these are not pulled in when you just call count().)  The need to
be careful with disctint() and ordering is noted here:

http://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct

The fix is to ensure default ordering does not interfere with distinct:

distinct_values = Model.objects.order_by().values('some_column').distinct()

Karen

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