1

我尝试对扩展字段进行过滤,正如以下示例所示,它实际上根本不是一个字段。为了这个例子,假设我们有一个正在运行的 Django,安装了 django-filer 并且已经上传了一些文件。

>>> from filer.models import File
>>> File.objects.all()[0].extension
'some_ext' # just as example

但是在尝试过滤时:

>>> File.objects.filter(extension='pdf')
django.core.exceptions.FieldError: Cannot resolve keyword 'extension'
into field. Choices are: _file_size, clipboarditem, description, 
downloadfilemodel, file, filer_image_file, folder, folder_id, 
has_all_mandatory_data, id, in_clipboards, is_public, modified_at, 
name, news_attachment, original_filename, owner, owner_id, 
polymorphic_ctype, polymorphic_ctype_id, sha1, uploaded_at

这是因为扩展不与模型一起存储,但实际上是计算属性。

我的问题:不将此元数据存储在文件模型中的理由是什么。

更新:我不抱怨实施。我询问实施的可能动机。

4

1 回答 1

1

扩展名不是一个非常重要的元数据,因为我们正在存储文件名。如果您查看源代码

@property
def extension(self):
    filetype = os.path.splitext(self.file.name)[1].lower()
    if len(filetype) > 0:
        filetype = filetype[1:]
    return filetype

您可以看到存储扩展意味着我们正在复制我们已经拥有的数据。因此决定将其作为财产。

于 2017-12-05T10:38:15.797 回答