gpt4 book ai didi

Deleting image related to a product from static folder when deleting product. Django(删除产品时,从静态文件夹中删除与产品相关的图像。姜戈)

转载 作者:bug小助手 更新时间:2023-10-24 17:55:12 27 4
gpt4 key购买 nike



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.

我还想知道从静态文件夹中删除无用文件是否符合逻辑。


更多回答

Does this answer your question? How to delete old image when update ImageField?

这回答了你的问题吗?如何在更新Imagefield时删除旧图像?

优秀答案推荐

Use the django-cleanup app to automatically delete files for FileField, ImageField and subclasses:

使用Django-Cleanup应用程序自动删除Filefield、Imagefield和子类的文件:



  1. Install:


pip install django-cleanup


  1. 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。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com