[Django] #35110: in_bulk does not work with annotated fields, nor on foreign key

8 views
Skip to first unread message

Django

unread,
Jan 12, 2024, 10:58:08 PMJan 12
to django-...@googlegroups.com
#35110: in_bulk does not work with annotated fields, nor on foreign key
-------------------------------------+-------------------------------------
Reporter: Cody | Owner: nobody
Towstik |
Type: Bug | Status: new
Component: Database | Version: 5.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
in_bulk does some checks about the fields on the model using the class
`_meta`.
It does not consider that fields can be annotated, or exist on another
related model.

However, the code that does all the processing seems like it could handle
both cases.

In both cases, you can make the field distinct.

class Person(models.Model):
email = models.CharField()

class Customer(models.Model):
person = models.ForeignKey("Person")

emails_to_process = [ "a", "b", "c" ]

# annotate case
Customer.objects.annotate(
email_lower=Lower("person__email")
).distinct(
"email_lower"
).in_bulk(
emails_to_process, fieldname="email_lower"
)

# foreign key case
Customer.objects.distinct("person__email").in_bulk(emails_to_process,
fieldname="person__email)

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

Django

unread,
Jan 12, 2024, 10:59:13 PMJan 12
to django-...@googlegroups.com
#35110: in_bulk does not work with annotated fields, nor on foreign key
-------------------------------------+-------------------------------------
Reporter: Cody Towstik | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Cody Towstik:

Old description:

> in_bulk does some checks about the fields on the model using the class
> `_meta`.
> It does not consider that fields can be annotated, or exist on another
> related model.
>
> However, the code that does all the processing seems like it could handle
> both cases.
>
> In both cases, you can make the field distinct.
>
> class Person(models.Model):
> email = models.CharField()
>
> class Customer(models.Model):
> person = models.ForeignKey("Person")
>
> emails_to_process = [ "a", "b", "c" ]
>
> # annotate case
> Customer.objects.annotate(
> email_lower=Lower("person__email")
> ).distinct(
> "email_lower"
> ).in_bulk(
> emails_to_process, fieldname="email_lower"
> )
>
> # foreign key case
> Customer.objects.distinct("person__email").in_bulk(emails_to_process,
> fieldname="person__email)

New description:

in_bulk does some checks about the fields on the model using the class
`_meta`.
It does not consider that fields can be annotated, or exist on another
related model.

However, the code that does all the processing seems like it could handle
both cases.

In both cases, you can make the field distinct.

{{{
class Person(models.Model):
email = models.CharField()

class Customer(models.Model):
person = models.ForeignKey("Person")

emails_to_process = [ "a", "b", "c" ]

# annotate case
Customer.objects.annotate(
email_lower=Lower("person__email")
).distinct(
"email_lower"
).in_bulk(
emails_to_process, fieldname="email_lower"
)

# foreign key case
Customer.objects.distinct("person__email").in_bulk(emails_to_process,
fieldname="person__email)
}}}

--

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

Django

unread,
Jan 15, 2024, 4:12:37 PMJan 15
to django-...@googlegroups.com
#35110: in_bulk does not work with annotated fields, nor on foreign key
-------------------------------------+-------------------------------------
Reporter: Cody Towstik | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* status: new => closed
* type: Bug => New feature
* version: 5.0 => dev
* resolution: => wontfix


Comment:

Hello Cody, thank you for your ticket. This seems to be a new feature
request since the documentation says that `in_bulk` works with `a unique
field or a distinct field` (I read this as model's own fields, not
annotations nor related field's fields, since checking uniqueness there
could get tricky/expensive).

For cases like this, the recommended path forward is to first propose and
discuss the idea/request with the community and gain consensus. To do
that, you could start a new conversation on the
[https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
reach a wider audience and likely get extra feedback.

I'll close the ticket for now, but if there is a community agreement for
the feature request, you are welcome to come back to the ticket and point
to the forum topic, so we can then re-open it. For more details, please
see [https://docs.djangoproject.com/en/stable/internals/contributing/bugs-
and-features/#requesting-features the documented guidelines for requesting
features].

Thanks!

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

Reply all
Reply to author
Forward
0 new messages