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
- Form field with fixed value by roam 3 days, 20 hours ago
- New Snippet! by Antoliny0919 1 week, 3 days ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 4 weeks ago
- get_object_or_none by azwdevops 6 months, 3 weeks ago
- Mask sensitive data from logger by agusmakmun 8 months, 2 weeks ago
Comments
Seems that this snippet cannot handle Field like MultiSelectField well.
#
Please login first before commenting.