#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070185da5132aa-e72d2e7d-d1b4-4db3-b2fa-2c5399fe0394-000000%40eu-central-1.amazonses.com.

Reply via email to