On 22 mar, 11:08, Kevin Renskers <i...@bolhoed.net> wrote:
> Hi,
>
> I am using a combination of the only() and distinct() functions on a
> model to get the unique values of one column. Sadly, the only()
> function also includes the primary key (even though I only give one
> column name), which has the effect that all rows are seen as unique.

only() is only (no pun) a complement to defer() - it still loads the
Model instances, so you'll indeed have the primary key included.

The method you want is values_list:

>> results = YourModel.objects.values_list('power_meter', flat=True).distinct()



> This is the result I want:
> power_meter
> 1
> 2
> 3
>
> This is the result I get:
> id      power_meter
> 80      1
> 81      1
> 82      1
> 83      2
> 84      2
> 85      3
> 86      3
>
> So I am wondering why the primary key is included in the query, even
> though I used only('power_meter') in my query.

cf above. Querysets default behaviour is to yield Model instances -
not raw data -, and Model instances neeed to have their primary key
loaded to work correctly. If you want raw data, use either values() or
values_list()

HTH

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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