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 Simon Charette, 4 years ago

Easy pickings: unset
Keywords: in_bulk, distinct, queryset → in_bulk distinct queryset
Triage Stage: UnreviewedAccepted
Type: BugNew feature

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 be self.query.distinct_fields == (field_name,) and not check for containment as suggested above.

Last edited 4 years ago by Simon Charette (previous) (diff)

comment:2 by Kaustubh, 4 years ago

Owner: changed from nobody to Kaustubh
Status: newassigned

I'd like to work on this

comment:3 by Wolph, 4 years ago

I was planning to create a PR over the weekend, but you're more than welcome to give it a try :)

in reply to:  3 comment:4 by Kaustubh, 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 Mariusz Felisiak, 4 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In b9be11d4:

Fixed #31918 -- Allowed QuerySet.in_bulk() to fetch on a single distinct field.

Note: See TracTickets for help on using tickets.
Back to Top