Clear F expressions after a model instance is saved
|Reported by:||codefisher||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.6|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I would have thought these two expresses would be exactly the same, except the second would be more efficient.
A: some_model.some_field += 1
B: some_model.some_field = F('some_field') + 1
I hit some unexpected behaviour when, I did this
# and somewhere far far away
With A, the field is incremented once, as I expected. But with B, it gets incremented twice. I would have expected the F query to effect the next database call, and nothing more. It might be intended, but then it is not documented.
I could write some come to demonstrate this. I only noticed it because I was using an import script that was creating lots of objects, and I had A and B in the model's overridden save method. They were causing vastly different results.
Change History (11)
comment:2 Changed 2 years ago by
|Component:||Database layer (models, ORM) → Documentation|
|Summary:||Unexpected result when incrimenting field with F expression → Document that F expressions are reapplied each time an object is saved|
|Triage Stage:||Unreviewed → Accepted|
|Type:||Uncategorized → Cleanup/optimization|
comment:9 Changed 7 months ago by
|Component:||Documentation → Database layer (models, ORM)|
|Summary:||Document that F expressions are reapplied each time an object is saved → Clear F expressions after a model instance is saved|