Skip to content

Model .save() with strange behaviour and not persisting on database #495

@IagoCarvalho

Description

@IagoCarvalho

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions