Skip to content

Instantly share code, notes, and snippets.

@cbess
Created December 4, 2011 23:21
Show Gist options
  • Save cbess/1431627 to your computer and use it in GitHub Desktop.
Save cbess/1431627 to your computer and use it in GitHub Desktop.
Update Django model from dictionary
def update_model(model, save_update=True, **kwargs):
"""Updates the specified model instance using the keyword arguments as the model
property attributes and values.
Example usage:
update_model(mymodel, save_update=True, **some_dictionary)
"""
for attr, val in kwargs.items():
setattr(model, attr, val)
if save_update:
model.save()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment