Opened 6 years ago

Last modified 6 years ago

#30053 closed New feature

Allow for conditional QuerySet.update_or_create() — at Initial Version

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

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 somevalue). 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:

  1. 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)
  2. Add another method, and re-write update_or_create() in terms of the new method with a condition that is always true.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top