I have a model:
我有一个模特:
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
is_digital = models.BooleanField(default=False, null=True, blank=False)
image = models.ImageField(null=True, blank=True)
def __str__(self) -> str:
return self.name
@property
def imageURL(self):
try:
url = self.image.url
except:
url = ''
return url
I want to delete the related image to this product when product deleted(for example from admin page).
I searched and found out that overriding del or delete functions are not the correct way to do that. Can you please help me.
我想删除该产品的相关图像时,产品删除(例如,从管理页面)。我搜索了一下,发现覆盖del或Delete函数不是正确的方法。你能帮帮我吗?
And I also wonder if it's even good logic to deleting unecessery files from static folder.
我还想知道从静态文件夹中删除无用文件是否符合逻辑。
更多回答
优秀答案推荐
Use the django-cleanup app to automatically delete files for FileField, ImageField and subclasses:
使用Django-Cleanup应用程序自动删除Filefield、Imagefield和子类的文件:
- Install:
pip install django-cleanup
- In settings.py add this after your apps:
INSTALLED_APPS = (
...,
'django_cleanup.apps.CleanupConfig',
)
Source: https://github.com/un1t/django-cleanup
消息来源:https://github.com/un1t/django-cleanup
更多回答
Thank you very much. It helped for me. I also used select docerator for specific model.
非常感谢。这对我很有帮助。我还针对特定型号使用了SELECT DOCERATOR。
我是一名优秀的程序员,十分优秀!