Login

Check If a Field Has Changed

Author:
zmsmith
Posted:
May 26, 2010
Language:
Python
Version:
1.2
Tags:
save field change check
Score:
2 (after 2 ratings)

Used to check if a field of an object has changed.

This is probably most useful in the save method.

1
2
3
4
5
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

More like this

  1. Update timestamp If any instance field has changed by craigruks 4 years ago
  2. Gravatar support in model save override by JoeLinux 2 years, 6 months ago
  3. Save image in field by grillermo 3 years, 7 months ago
  4. is_dirty and dict of changed values by jaredthane 6 years ago
  5. "Approved" field with timestamp by miracle2k 8 years, 1 month ago

Comments

Please login first before commenting.