django-polymorphic
django-polymorphic copied to clipboard
Model .save() with strange behaviour and not persisting on database
Hi, the problem i was facing Is kinda straightforward:
I have 3 models:
Class B(models.model): datefield.... datefield....
Class A(PolymorphicModel, Class B): ....
Class C(Class A): ....
Class C has several properties and methods but one method in particular isn't able to actually save the data when doing a simple save operation like:
def foo(self, value):
self.bar += value
self.save(update_fields=['bar'])
After calling the method or in my serializer's save method:
obj.bar
> "bar + value"
After the response from serializer data or in the unit test (even after refresh_from_db()):
obj.bar
> old value
My ugly workaround:
def foo(self, value):
obj = self.__class__.objects.get(id=self.id)
obj.bar += value
obj.save(update_fields=['bar'])
This particular method works normally with this. Considerations: It wasn't an atomic block, It didn't raise any exceptions or errors, the obj existed and had a pk, it was doing a simple update.
Class A(PolymorphicModel, Class B):
This is incorrect. Inherit from PolymorphicModel instead of models.Model at the base - it is not a mixin.