我不确定是否可以回答自己的问题,但我只是想出了(我认为)。
根据此错误报告,我编辑了代码:
from django.utils.deconstruct import deconstructible@deconstructibleclass PathAndRename(object): def __init__(self, sub_path): self.path = sub_path def __call__(self, instance, filename): ext = filename.split('.')[-1] # set filename as random string filename = '{}.{}'.format(uuid4().hex, ext) # return the whole path to the file return os.path.join(self.path, filename)path_and_rename = PathAndRename("/avatars")然后,在字段定义中:
avatar = models.ImageField(upload_to=path_and_rename, null=True, blank=True, default="avatars/none/default.png", height_field="image_height", width_field="image_width")
这对我有用。



