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
- Serializer factory with Django Rest Framework by julio 5 months, 4 weeks ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 6 months, 2 weeks ago
- Help text hyperlinks by sa2812 7 months, 1 week ago
- Stuff by NixonDash 9 months, 2 weeks ago
- Add custom fields to the built-in Group model by jmoppel 11 months, 3 weeks ago
Comments
Seems that this snippet cannot handle Field like MultiSelectField well.
#
Please login first before commenting.