-
-
Save NurElHuda/f4db576fb04ad43b21c6a1963b171476 to your computer and use it in GitHub Desktop.
Django refresh and update model instance helpers
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 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
Very helpful, just what I was looking for. Thank you!