Created
December 4, 2011 23:21
-
-
Save cbess/1431627 to your computer and use it in GitHub Desktop.
Update Django model from dictionary
This file contains hidden or 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_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