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

前端 未结 25 1837
鱼传尺愫
鱼传尺愫 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:39

    You can use django-model-changes to do this without an additional database lookup:

    from django.dispatch import receiver
    from django_model_changes import ChangesMixin
    
    class Alias(ChangesMixin, MyBaseModel):
       # your model
    
    @receiver(pre_save, sender=Alias)
    def do_something_if_changed(sender, instance, **kwargs):
        if 'remote_image' in instance.changes():
            # do something
    

提交回复
热议问题