Created
June 1, 2012 22:50
-
-
Save rmaceissoft/2855599 to your computer and use it in GitHub Desktop.
update_if_changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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