[英]Django filer image related to another model, i.e. is it being used?
I am creating a cleanup script for Django Filer, so that if an image is not in use, eg it's not related to any other model, then I'd like to delete the image. 我正在为Django Filer创建清理脚本,因此,如果未使用图像,例如,它与任何其他模型都不相关,那么我想删除该图像。
I can't figure out how to detect if the image is related to any other objects though. 我无法弄清楚如何检测图像是否与其他任何对象有关。 Does anybody know how to achieve this?
有人知道如何实现这一目标吗?
You can view this information when you press the delete button and are on the delete confirmation page, but I'm not sure how Django has done this. 当您按下Delete按钮并且位于Delete Confirmation页面上时,您可以查看此信息,但是我不确定Django如何做到这一点。
Any help would be much appreciated. 任何帮助将非常感激。
To clarify, 澄清,
from filer.models import Image
i = Image.objects.all().first()
i.get_all_related_objects.count()
The last line of the above is not valid, but this is what I'm trying to achieve, so if this is 0, then I can remove the file. 上面的最后一行无效,但这是我要达到的目的,因此如果该值为0,则可以删除该文件。
You can get all related objects, no matter from what related model they come, using 您可以使用以下任何一种相关模型来获取所有相关对象:
instance._meta.get_all_related_objects()
so when assuming you have an Image
model: 因此,假设您拥有
Image
模型:
for image in Image.objects.all():
if not image._meta.get_all_related_objects(): # if this list is empty, there are no relations pointing here
image.delete()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.