django - 在 Django 2.2 中自定义 ImageField 和 FileField 的上传路径的问题
问题描述
我在模型外部创建了一个函数,用作实用函数,只需将path
字符串传递给它即可与任何方法一起使用,它会返回文件路径和重命名的文件名。更改文件名的包装在接受字符串function(instance, filename)
的包装函数中。path
这是函数(存储helpers.py
在另一个应用程序中):
def path_and_rename(path):
"""
Returns wrapper func
:param path: path string with slash at the end
:return: func
"""
def wrapper(instance, filename):
"""
Returns a filename string, both
and filename, with filename as an md5 string
:param instance: model instance with the file_field or image_field
:param filename: filename as uploaded (as received from the model)
:return: str
"""
ext = filename.split('.')[-1] # Preserve image file extension
md5_file_name = f"{hashlib.md5(str(filename).encode('utf-8')).hexdigest()}.{ext}" # md5 from filename
return f"{path}{md5_file_name}"
return wrapper
在我的模型中,我做了以下事情:
image = ImageField(verbose_name=_("Product image"), upload_to=path_and_rename("products/images/"))
但这会产生错误makemigrations
:
'Could not find function %s in %s.\n' % (self.value.__name__, module_name)
ValueError: Could not find function wrapper in my_app_root.core.helpers.
解决方案
这有点棘手。Djangomakemigrations
命令尝试以编程方式生成迁移文件。
当您将函数upload_to
或default
关键字参数传递给模型字段时,它会在迁移文件中导入包含该函数的整个模块。因此,在您的情况下,Django 将在它要生成的迁移文件之上编写以下导入。
import my_app_root.core.helpers
之后,它将尝试从导入的模块中获取对该函数的引用。__qualname__
因为在您的情况下,最终将用于获取路径的wrapper
函数由另一个函数返回,django 将尝试做my_app_root.core.helpers.wrapper
这将(并且)肯定会失败。
所以最终的解决方案是使用模块级函数作为upload_to
参数的参考。然而,一个有点棘手(并且可能很难看)的解决方案可以将函数调用分配给一个变量,并为它分配一个__qualname__
像这样的同名。
def path_and_rename(path):
# all the functionality here
product_image_upload_path = path_and_rename('products/images/')
# assign it `__qualname__`
product_image_upload_path.__qualname__ = 'product_image_upload_path'
然后像这样在模型字段中使用这个变量。
image = ImageField(upload_to=product_image_upload_path)
推荐阅读
- c# - C# 方法重载而不重写内容
- html - 在较小的屏幕尺寸上控制 flexbox
- django - 如何从具有 ForeignKey 关系的另一个模型中检索模型列表
- error-handling - 530, "5.7.0", 必须先发出 STARTTLS 命令
- c - 从不同目录链接外部库
- javascript - 裁剪边框内的图像(删除外面的所有内容)
- networking - 如何拦截来自我的 C 应用程序的所有网络调用?
- python - AWS Lambda S3 复制到 Postgres 成功但不复制数据
- python - pgadmin4 安装 Centos 7
- typo3 - TYPO3 中的 HTML 模板文件在哪里存储?