Opened 7 years ago

Closed 7 years ago

#28240 closed Bug (needsinfo)

django.db.models.query.values not acting as documented

Reported by: Meckl Antal Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello, I'm using .values() to select distinct values and annotate them with a Count.
Here is the example code:

MyObjects.objects.values('image')
QuerySet [
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'dbd582eb-c8b6-45d8-8787-24fc8467cbce'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'image': u'dbd582eb-c8b6-45d8-8787-24fc8467cbce'}, 
{'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}]

The documentation says:

...you will only get an annotated result for each unique author name...

Change History (4)

comment:1 by Tim Graham, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)

Your example code doesn't include an annotate()?

comment:2 by Meckl Antal, 7 years ago

Yes, I know, I forgot to place it. But I think it doesn't matter, the values() should return unique values.
Here is the code with annotate.

MyObject.objects.values('image').exclude(terminated_at__isnull=False).annotate(count=Count('id'),)
QuerySet [
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'dbd582eb-c8b6-45d8-8787-24fc8467cbce'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'},
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'},
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'},
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'},
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'},
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}, 
{'count': 1, 'image': u'dbd582eb-c8b6-45d8-8787-24fc8467cbce'}, 
{'count': 1, 'image': u'40476fd4-ad65-4846-b307-51ddf901da53'}]

comment:3 by Tim Graham, 7 years ago

If there is something in the documentation in QuerySet.values() documentation that implies it returns distinct values, please let us know. That shouldn't be the case.

As for your issue, please include a complete test case with minimal models and data.

comment:4 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top