Django admin file upload with current model id

前端 未结 5 1047
轮回少年
轮回少年 2020-12-01 03:29

I\'m trying to create a simple photo gallery with the default Django admin. I\'d like to save a sample photo for each gallery, but I don\'t want to keep the filname. Instead

5条回答
  •  甜味超标
    2020-12-01 03:45

    I ran into the same problem. Okm's answer sent me on the right path but it seems to me it is possible to get the same functionality by just overriding the save() method of your Model.

    def save(self, *args, **kwargs):
        if self.pk is None:
            saved_image = self.image
            self.image = None
            super(Material, self).save(*args, **kwargs)
            self.image = saved_image
    
        super(Material, self).save(*args, **kwargs)
    

    This definitely saves the information correctly.

提交回复
热议问题