When saving, how can you check if a field has changed?

前端 未结 25 1767
鱼传尺愫
鱼传尺愫 2020-11-22 07:15

In my model I have :

class Alias(MyBaseModel):
    remote_image = models.URLField(max_length=500, null=True, help_text=\"A URL that is downloaded and cached          


        
25条回答
  •  有刺的猬
    2020-11-22 07:34

    How about using David Cramer's solution:

    http://cramer.io/2010/12/06/tracking-changes-to-fields-in-django/

    I've had success using it like this:

    @track_data('name')
    class Mode(models.Model):
        name = models.CharField(max_length=5)
        mode = models.CharField(max_length=5)
    
        def save(self, *args, **kwargs):
            if self.has_changed('name'):
                print 'name changed'
    
        # OR #
    
        @classmethod
        def post_save(cls, sender, instance, created, **kwargs):
            if instance.has_changed('name'):
                print "Hooray!"
    

提交回复
热议问题