[Django] #31918: in_bulk not working for distinct fields

23 views
Skip to first unread message

Django

unread,
Aug 20, 2020, 10:27:44 AM8/20/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) | Keywords: in_bulk, distinct,
Severity: Normal | queryset
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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`

--
Ticket URL: <https://code.djangoproject.com/ticket/31918>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 20, 2020, 2:13:03 PM8/20/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Accepted
queryset |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* keywords: in_bulk, distinct, queryset => in_bulk distinct queryset
* type: Bug => New feature
* easy: 1 => 0
* stage: Unreviewed => Accepted


Comment:

Feature request makes sense, PR with tests is welcome.

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:1>

Django

unread,
Aug 21, 2020, 11:22:11 AM8/21/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: assigned

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Accepted
queryset |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Kaustubh):

* owner: nobody => Kaustubh
* status: new => assigned


Comment:

I'd like to work on this

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:2>

Django

unread,
Aug 21, 2020, 11:23:56 AM8/21/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Accepted
queryset |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Wolph):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:3>

Django

unread,
Aug 21, 2020, 12:01:47 PM8/21/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Accepted
queryset |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Kaustubh):

Replying to [comment:3 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)

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:4>

Django

unread,
Aug 22, 2020, 9:39:02 AM8/22/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Accepted
queryset |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Kaustubh):

Linked PR : https://github.com/django/django/pull/13338

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:5>

Django

unread,
Aug 26, 2020, 3:42:16 AM8/26/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: in_bulk distinct | Triage Stage: Ready for
queryset | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:6>

Django

unread,
Aug 26, 2020, 5:16:32 AM8/26/20
to django-...@googlegroups.com
#31918: in_bulk not working for distinct fields
-------------------------------------+-------------------------------------
Reporter: Wolph | Owner: Kaustubh
Type: New feature | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: in_bulk distinct | Triage Stage: Ready for
queryset | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b9be11d44265308863e4e8cfb458cd3605091452" b9be11d4]:
{{{
#!CommitTicketReference repository=""
revision="b9be11d44265308863e4e8cfb458cd3605091452"
Fixed #31918 -- Allowed QuerySet.in_bulk() to fetch on a single distinct
field.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31918#comment:7>

Reply all
Reply to author
Forward
0 new messages