首页 > 解决方案 > 在 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.

标签: djangopython-3.xdjango-modelspython-decoratorshigher-order-functions

解决方案


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

当您将函数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)

推荐阅读