if not created and modified>instance.modified:
for key, value in defaults_dict.items():
setattr(instance, key, value)
instance.save()
Now this extra clause can be fairly arbitrary.
A generic piece of code could check if there's any difference
between the defaults dict and the instance, then update those
differences and save the instance. Drawback is that wouldn't be very
fast but at least better then continuously saving the model imho.
def create_or_update(...):
updated = False
obj, created = Model.objects.get_or_create(something=something, ...,
defaults_dict = defaults_dict)
if not created and any(value!=getattr(obj, key) for key, value in
defaults_dict.items()):
# TODO: only update differences here (nicer then updating all?)
for key, value in defaults_dict.items():
setattr(obj, key, value)
obj.save()
updated = True
return obj, created, updated
Guess something like this could/would also be a nice snipplet to put on
django snipplets :)