[Django] #34280: Support create defaults for update_or_create

8 views
Skip to first unread message

Django

unread,
Jan 22, 2023, 11:30:35 AM1/22/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy | Owner: nobody
Schilling |
Type: New | Status: assigned
feature |
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 |
-------------------------------------+-------------------------------------
I proposed the idea of extending `update_or_create` to support specifying
a different set of defaults for the create operation on the
[forum](https://forum.djangoproject.com/t/feature-idea-update-or-create-
to-allow-different-defaults-for-create-and-update-operations/18300/15).
There seems to be consensus it's a positive add to Django.

Adam raised concerns with my proposed approach of adding a
`create_defaults` parameter to the function since this would conflict with
any fields on a model named, `create_defaults`. Jeff did a code search on
github for that term and didn't find any matches. I suspect if someone
where using a field named `create_defaults`, it would be a JSON or object
type field. Those don't seem like reasonable candidates to be part of a
`UniqueConstraint`, which should be underlying the look-up arguments to
`update_or_create`.

I do like the idea of having a separate parameter for `create_defaults`,
but if we must preserve 100% backwards compatibility, Adam's suggestion of
having defaults be set to another object makes the most sense.

My blocking question is, which approach should I take?

From the forum post:

I’ve run into a use-case in which it’d be helpful to have the ability
to specify a different set of defaults for the update operation compared
to the create operation. While I don’t expect my particular use case to
translate, here’s a more generic one.
Given the following Record model:
{{{
class Record(models.Model):
some_id = models.CharField(unique=True)
created_by = models.ForeignKey(User, ...)
modified_by = models.ForeignKey(User, null=True, blank=True, ...)
}}}
When a record is created, we would want to set created_by, but if it’s
being updated, we’d want to set modified_by. This use case can’t be solved
by using update_or_create, unless it allows for us to specify a different
set of default values.
{{{
Record.objects.update_or_create(
some_id=some_value,
defaults={"modified_by": user},
create_defaults={"created_by": user},
)
}}}

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

Django

unread,
Jan 22, 2023, 11:33:51 AM1/22/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(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
-------------------------------------+-------------------------------------
Changes (by Timothy Schilling):

* owner: nobody => Timothy Schilling


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

Django

unread,
Jan 23, 2023, 5:24:50 AM1/23/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted


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

Django

unread,
Jan 30, 2023, 9:41:15 PM1/30/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | 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 Timothy Schilling):

* has_patch: 0 => 1


Comment:

Here's a PR with the first version of the changes.
https://github.com/django/django/pull/16511

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

Django

unread,
Jan 31, 2023, 6:09:03 AM1/31/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1

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

* needs_docs: 0 => 1


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

Django

unread,
Feb 4, 2023, 11:53:17 AM2/4/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Timothy Schilling):

I ended up going with the `create_defaults` approach since we have a
workaround for the lookup via `create_defaults__exact`.

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

Django

unread,
Feb 14, 2023, 5:52:20 AM2/14/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | 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 Mariusz Felisiak):

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


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

Django

unread,
Feb 14, 2023, 6:39:32 AM2/14/23
to django-...@googlegroups.com
#34280: Support create defaults for update_or_create
-------------------------------------+-------------------------------------
Reporter: Timothy Schilling | Owner: Timothy
| Schilling
Type: New feature | 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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"c5808470aaffda661cb911b06d5b848dd7b75467" c580847]:
{{{
#!CommitTicketReference repository=""
revision="c5808470aaffda661cb911b06d5b848dd7b75467"
Fixed #34280 -- Allowed specifying different field values for create
operation in QuerySet.update_or_create().
}}}

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

Reply all
Reply to author
Forward
0 new messages