[Django] #36987: UUIDField.get_db_prep_value does not observe prepared argument

2 views
Skip to first unread message

Django

unread,
Mar 16, 2026, 9:31:28 AM (2 days ago) Mar 16
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob | Owner: Jacob Walls
Walls |
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The
[https://docs.djangoproject.com/en/6.0/ref/models/fields/#django.db.models.Field.get_db_prep_value
doc'd "default" pattern] for fields is for `get_db_prep_value()` to call
the `get_prep_value()` method if `prepared` is False. The idea being, I
assume, to avoid wasted computation if the value is already
`prepared=True`.

`UUIDField` doesn't follow this pattern, causing two extra calls to
`isinstance(val, UUID)` per value, which can add up for a large bag of
values, e.g. a `prefetch_related()` call.

Here is a [https://dryorm.xterm.info/uuidfield-get-db-prep-value fiddle]
on DryORM with my fix branch checked out. Re-run the fiddle against main
and notice 4 fewer calls to isinstance (2 * n objects).

Discovered while reviewing the profile in a
[https://github.com/django/django/pull/20779/changes micro-optimization
PR]
----

I acknowledge there is some exploratory effort toward removing this
argument in a [https://github.com/django/django/pull/19051 draft PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/36987>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 16, 2026, 9:34:36 AM (2 days ago) Mar 16
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

[https://github.com/django/django/pull/20901 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36987#comment:1>

Django

unread,
Mar 16, 2026, 9:35:05 AM (2 days ago) Mar 16
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Jacob Walls:

Old description:

> The
> [https://docs.djangoproject.com/en/6.0/ref/models/fields/#django.db.models.Field.get_db_prep_value
> doc'd "default" pattern] for fields is for `get_db_prep_value()` to call
> the `get_prep_value()` method if `prepared` is False. The idea being, I
> assume, to avoid wasted computation if the value is already
> `prepared=True`.
>
> `UUIDField` doesn't follow this pattern, causing two extra calls to
> `isinstance(val, UUID)` per value, which can add up for a large bag of
> values, e.g. a `prefetch_related()` call.
>
> Here is a [https://dryorm.xterm.info/uuidfield-get-db-prep-value fiddle]
> on DryORM with my fix branch checked out. Re-run the fiddle against main
> and notice 4 fewer calls to isinstance (2 * n objects).
>
> Discovered while reviewing the profile in a
> [https://github.com/django/django/pull/20779/changes micro-optimization
> PR]
> ----
>
> I acknowledge there is some exploratory effort toward removing this
> argument in a [https://github.com/django/django/pull/19051 draft PR].

New description:

The
[https://docs.djangoproject.com/en/6.0/ref/models/fields/#django.db.models.Field.get_db_prep_value
doc'd "default" pattern] for fields is for `get_db_prep_value()` to call
the `get_prep_value()` method if `prepared` is False. The idea being, I
assume, to avoid wasted computation if the value is already
`prepared=True`.

`UUIDField` doesn't follow this pattern, causing two extra calls to
`isinstance(val, UUID)` per value, which can add up for a large bag of
values, e.g. a `prefetch_related()` call.

Here is a [https://dryorm.xterm.info/uuidfield-get-db-prep-value fiddle]
on DryORM with my fix branch checked out. Re-run the fiddle against main
and notice 4 more calls to isinstance (2 * n objects).

Discovered while reviewing the profile in a
[https://github.com/django/django/pull/20779/changes micro-optimization
PR]
----

I acknowledge there is some exploratory effort toward removing this
argument in a [https://github.com/django/django/pull/19051 draft PR].

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

Django

unread,
Mar 16, 2026, 10:51:53 AM (2 days ago) Mar 16
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

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

Django

unread,
Mar 17, 2026, 10:21:45 AM (yesterday) Mar 17
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

Comment:

Clifford approved the PR on GitHub.
--
Ticket URL: <https://code.djangoproject.com/ticket/36987#comment:4>

Django

unread,
8:22 AM (8 hours ago) 8:22 AM
to django-...@googlegroups.com
#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

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

Comment:

In [changeset:"2e33abe57c37dce6ce5eff9e6cbaf47cdddcf1e3" 2e33abe]:
{{{#!CommitTicketReference repository=""
revision="2e33abe57c37dce6ce5eff9e6cbaf47cdddcf1e3"
Fixed #36987 -- Observed prepared argument in
UUIDField.get_db_prep_value().

This avoids two isinstance() calls per UUID value.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36987#comment:5>
Reply all
Reply to author
Forward
0 new messages