Skip to content

Instantly share code, notes, and snippets.

@charlwillia6
Last active March 5, 2019 21:47
Show Gist options
  • Save charlwillia6/512fee1dce086bc90f2d1fe1cfc08f33 to your computer and use it in GitHub Desktop.
Save charlwillia6/512fee1dce086bc90f2d1fe1cfc08f33 to your computer and use it in GitHub Desktop.
Field Change - Check if a field has changed before saving a model. #django #python #view
def has_changed(instance, field):
if not instance.pk:
return False
old_value = instance.__class__._default_manager.filter(pk=instance.pk).values(field).get()[field]
return not getattr(instance, field) == old_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment