resolve_computed_field does not consider update_fields from signal pre_save
URUKOVV opened this issue · comments
Would be better to consider that)
That's a good idea! To clarify what you're suggesting:
If update_fields is None
, or the computed field name appears, then compute the value and save to the database.
If update_fields
is set to some other set of fields not including the computed field, then don't save it to the database.
So, a contrived example to show a possibly confusing side effect of this:
class Foo(models.Model):
value = models.IntegerField()
doubled = ComputedIntegerField(compute_from='compute_val')
@property
def compute_val(self):
return 2 * self.value
my_foo = Foo.objects.create(value=4)
# (db should have value=4, doubled=8)
my_foo.value = 5
# my_foo.doubled == 10
my_foo.save(update_fields=['value'])
# (db has value=5, doubled=8) <--- this is what might be surprising
I can where there might be situations where this is desired. Is this the behavior that you are after?
The situation I encountered is this: for a new computed_field i should implement 'compute_from' function in the migration file for every computed fields implemented before, could not just import these functions couse of migration and code compatability, and thats disgusting. U just showed the other side effect, thank you!