Sunday, September 23, 2012

Check if field is changed before saving entry


When you save entry in Django, how to check, if a field is changed?
Here is simple solution:
class MyModel(models.Model):
    title = models.CharField(max_length=1)

    def save(self, *args, **kw):
        if self.pk is not None:
            orig = MyModel.objects.get(pk=self.pk)
            if orig.title != self.title:
                print 'title is changed'
        super(MyModel, self).save(*args, **kw)

No comments:

Post a Comment