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.
* owner: nobody => Timothy Schilling
--
Ticket URL: <https://code.djangoproject.com/ticket/34280#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/34280#comment:2>
* 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>
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34280#comment:4>
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>
* needs_docs: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34280#comment:6>
* 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>