Created
October 9, 2012 16:43
-
-
Save kipanshi/3859962 to your computer and use it in GitHub Desktop.
Django refresh and update model instance helpers
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 refresh(instance): | |
"""Select and return instance from database. | |
Usage: ``instance = refresh(instance)`` | |
""" | |
return instance.__class__.objects.get(pk=instance.pk) | |
def update(instance, **data): | |
"""Update instance with data directly by using ``update()`` | |
skipping calling ``save()`` method. | |
Usage: ``instance = update(instance, some_field=some_value)`` | |
""" | |
instance.__class__.objects.filter(pk=instance.pk).update(**data) | |
return refresh(instance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment