Opened 4 years ago
Closed 4 years ago
#31918 closed New feature (fixed)
in_bulk not working for distinct fields
Reported by: | Wolph | Owned by: | Kaustubh |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.1 |
Severity: | Normal | Keywords: | in_bulk distinct queryset |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The in_bulk
queryset function currently checks for the pk
field or for unique
fields. But if you do a queryset.distinct(field)
that field is also unique for that queryset so it should be usable for an in_bulk
.
I believe the fix would be as easy as patching these lines: https://github.com/django/django/blob/41725602afebe2ddb018b99afe134384cc3bf69e/django/db/models/query.py#L699-L703
To include and field_name not in self.query.distinct_fields
Change History (7)
comment:1 by , 4 years ago
Easy pickings: | unset |
---|---|
Keywords: | in_bulk, distinct, queryset → in_bulk distinct queryset |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → New feature |
follow-up: 4 comment:3 by , 4 years ago
I was planning to create a PR over the weekend, but you're more than welcome to give it a try :)
comment:4 by , 4 years ago
Replying to Wolph:
I was planning to create a PR over the weekend, but you're more than welcome to give it a try :)
Thanks, I'll inform you if I get stuck (I'm a newcomer to open source)
comment:6 by , 4 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Feature request makes sense, PR with tests is welcome.
Note that this only works if there's a single field being distinct on;
distinct('foo', 'bar').in_bulk(field_name='foo')
should not work. In other words the logic should beself.query.distinct_fields == (field_name,)
and not check for containment as suggested above.