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

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

    I had this situation before my solution was to override the pre_save() method of the target field class it will be called only if the field has been changed
    useful with FileField example:

    class PDFField(FileField):
        def pre_save(self, model_instance, add):
            # do some operations on your file 
            # if and only if you have changed the filefield
    

    disadvantage:
    not useful if you want to do any (post_save) operation like using the created object in some job (if certain field has changed)

提交回复
热议问题