[Answer]-How to see if a field changed in model save method

1👍

I think the best solution is use django model pre_save signal.

Before save, instance in db is still original one, but instance param has the new values, so you can check if a field has changed.

from django.db import models
from django.dispatch import receiver

@receiver(models.signals.pre_save, sender=Answer)
def prepare_save(sender, instance, **kwargs):
    try:
        current_instance = sender.objects.get(pk=instance.pk)
        if current_instance.title != instance.title:
            print 'Title changed to %s!' % instance.title
    except sender.DoesNotExist:
        print 'new answer. No title change'

Leave a comment