#30053: Allow for conditional QuerySet.update_or_create()
-------------------------------------+-------------------------------------
Reporter: Joshua Cannon | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: QuerySet | Triage Stage:
update_or_create | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Joshua Cannon):
Replying to [comment:4 Joshua Cannon]:
> Replying to [comment:2 Simon Charette]:
> > Could you detail your use case a bit more? I'm not sure I understand
how adding a condition to the `if created` statement would allow you to
put updates behind a condition given it's only reached when the row gets
created.
> >
> > Also, have you experimented with chaining
`filter().update_or_create()` to limit its application to a filtered
domain?
>
> Oohh. good observation (See, I'm already introducing bugs in my solution
and I'm aware of the problem!). Modified the details.
>
> Also, no I haven't. Assuming that works and behaves as expected that
might be the solution I'm looking for. If that's the case, perhaps a note
in the documentation would be beneficial to novice/intermediate developers
like myself.
OK, {{{filter().create_or_update()}}} isn't the same, as it would create a
new instance if the condition fails, but the kwargs would've matched.
E.g.:
{{{
class MyModel(models.Model):
magic_number = models.IntegerField(default=0)
should_be_updated = models.BooleanField(default=True)
MyModel.objects.create(magic_number=42, should_be_updated=False)
# The following should be a no-op
MyModel.objects.update_or_create(magic_number=42, condition=lambda m:
m.should_be_updated)
# The following creates a new instance
MyModel.objects.filter(should_be_updated=True).update_or_create(magic_number=42)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30053#comment:5>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.263719392e231f45d3795096dad83811%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.