简体   繁体   English

与另一个模型相关的Django文件管理器图像,即是否正在使用它?

[英]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. 任何帮助将非常感激。

Edit 编辑

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.

相关问题 如何按类别字段扩展 Django 文件管理器图像模型? - How to extend Django filer image model by category field? 无法在Django中正确设置与模型相关的图像的路径 - Not being able to set the path for an image related to a model properly in Django 使用Django Filer的Django 1.11图像库 - Django 1.11 Image Gallery with Django Filer Django过滤与另一个模型相关的模型对象 - Django filter model objects related to another model 顺序Keras模型能否获得未展平的多维输入(例如图像)(即,输入形状是每个图像MxN像素)? - Can a Sequential Keras-model get multidimensional input (such as an image) that is not flattened (i.e. input shape is MxN pixels per image)? 保存的图像无法在 Django 文件管理器中显示 - Saved image cannot be displayed in Django filer 偶尔,Django消息会在请求中重复(即,它们不会被清除) - Occasionally, Django messages are repeated across requests (i.e., they are not cleared) Select 仅与 Django 中的另一个 model 相关的选项 - Select only the options that are related to another model in Django 如何在get_context_data中执行两个相关的数据库查询,即使用“ request”? - How can perform two related database query in get_context_data i.e. use “request”? Django删除另一个模型中没有更多相关外键项的模型实例? - Django deleting a model instance with no more related ForeignKey items in another model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM