在Django 2.2中自定义ImageField和FileField的上传路径时出现问题

问题描述 投票:0回答:1

我在模型外部创建了一个函数,用作实用函数,只需将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.
django python-3.x django-models python-decorators higher-order-functions
1个回答
0
投票

这有点棘手。 Django makemigrations命令尝试以编程方式生成迁移文件。

将函数传递给模型字段的upload_todefault关键字参数时,它会在迁移文件中导入包含该函数的整个模块。因此,在您的情况下,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)
© www.soinside.com 2019 - 2024. All rights reserved.