Opened 7 years ago
Last modified 7 years ago
#30053 closed New feature
Allow for conditional QuerySet.update_or_create() — at Version 1
| Reported by: | Joshua Cannon | Owned by: | nobody | 
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | Normal | Keywords: | QuerySet update_or_create | 
| Cc: | Nasir Hussain, Adam Johnson | Triage Stage: | Unreviewed | 
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description (last modified by )
QuerySet.update_or_create() is great since it lifts the burden of thread-safety and repeated code from the client. However there exists some scenarios where the "update" logic should be hidden behind a condition. (E.g. only update if the value of a DateTimeField is less than some value). There isn't much help for clients in these scenarios, with the best solution being to copy+paste QuerySet.update_or_create() and add a condition to the if created: expression.
The condition would most likely be a callable that accepts one argument, the gotten instance.
The solution should be trivial, either:
- Add a "condition" parameter to 
update_or_create()(along with logic to make the feature backwards compatible, likely checking if the value is callable with one argument or not, since "condition" could be a field) - Add another method, and re-write 
update_or_create()in terms of the new method with a condition that is always true.