Skip to content

Instantly share code, notes, and snippets.

@rmaceissoft
Created June 1, 2012 22:50
Show Gist options
  • Save rmaceissoft/2855599 to your computer and use it in GitHub Desktop.
Save rmaceissoft/2855599 to your computer and use it in GitHub Desktop.
update_if_changed
def update_if_changed(model_instance, new_values):
has_changed = False
for field_name, field_value in new_values.items():
old_value = getattr(model_instance, field_name)
if old_value != field_value:
setattr(model_instance, field_name, field_value)
has_changed = True
if has_changed:
model_instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment