How do I know if django model instance was modified?

django, django-models

Solution

The typical pattern is to do something like:

model = Model.objects.get(pk=2342)
dirty = False
if foo:
    model.foo = 'bar'
    dirty = True
if bar:
    model.bar = 'baz'
    dirty = True

if dirty:
    model.save()

Problem

I've got a code like that: ``` # ... obj = Model.objects.get(pk=2342) if foo: obj.foo = 'bar' if bar: obj.bar = 'baz' obj.save() ``` Is there a good way to find out if the model instance was modified in order to prevent saving it each time the code runs?

Original source