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

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

    If you are using a form, you can use Form's changed_data (docs):

    class AliasForm(ModelForm):
    
        def save(self, commit=True):
            if 'remote_image' in self.changed_data:
                # do things
                remote_image = self.cleaned_data['remote_image']
                do_things(remote_image)
            super(AliasForm, self).save(commit)
    
        class Meta:
            model = Alias
    

提交回复
热议问题