[Django] #34282: Optimize update_or_create when defaults is empty / False-y

4 views
Skip to first unread message

Django

unread,
Jan 22, 2023, 11:39:46 AM1/22/23
to django-...@googlegroups.com
#34282: Optimize update_or_create when defaults is empty / False-y
-------------------------------------+-------------------------------------
Reporter: Timothy | Owner: Timothy Schilling
Schilling |
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: dev
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 |
-------------------------------------+-------------------------------------
The defaults parameter to `update_or_create` is used to change the values
on a model instance after it's been fetched from the database. However,
it's not required to pass anything in through `defaults`. In these cases
where `defaults` is False-y, Django is performing an update on the model
instance that shouldn't be changing any values. Instead the call to
[object.save()](https://github.com/django/django/blob/stable/4.2.x/django/db/models/query.py#L971-L972)
should be skipped.

The concern would be needing to check for fields that use something like
`auto_now`.

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

Django

unread,
Jan 23, 2023, 2:48:07 AM1/23/23
to django-...@googlegroups.com
#34282: Optimize update_or_create when defaults is empty / False-y
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
Type: | Schilling
Cleanup/optimization | 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 Mariusz Felisiak):

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


Comment:

Thanks for this ticket. An empty set of `defaults` is always a subset of
`concrete_field_names` so
[https://github.com/django/django/blob/0fd5d16c222583dd68128b70b17e461d4decda8c/django/db/models/query.py#L971-L972
the "else" branch] is not reachable in this case. We could skip
[https://github.com/django/django/blob/0fd5d16c222583dd68128b70b17e461d4decda8c/django/db/models/query.py#L970
save()] when `update_fields` is empty but this would be backward
incompatible e.g. for users with database triggers. I'm not convinced it's
worth changing, users who don't want to update any fields should use
`get_or_create()` instead. Hope that makes sense.

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

Reply all
Reply to author
Forward
0 new messages