Skip to content

Instantly share code, notes, and snippets.

@kipanshi
Created October 9, 2012 16:43
Show Gist options
  • Save kipanshi/3859962 to your computer and use it in GitHub Desktop.
Save kipanshi/3859962 to your computer and use it in GitHub Desktop.
Django refresh and update model instance helpers
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