gpt4 book ai didi

Django 无法进行迁移

转载 作者:行者123 更新时间:2023-12-04 15:35:40 32 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Django - Cannot create migrations for ImageField with dynamic upload_to value

(3 个回答)


去年关闭。




我正在尝试迁移到uploadExcel,但出现错误提示

ValueError: Could not find function wrapper in uploadExcel.models.



这是我在我的uploadExcel.models 中使用的代码。
from django.db import models
import os

def path_and_rename(path):
def wrapper(instance, filename):
ext = filename.split('.')[-1]
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
filename = '{}.{}'.format('data', ext)
return os.path.join(path, filename)
return wrapper



class ExcelUploadModel(models.Model):

file_name = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to=path_and_rename('test/'))
uploaded_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.file_name

def delete(self,*args, **kwargs):
self.document.delete()
super().delete(*args, **kwargs)

任何帮助将不胜感激
谢谢你
最好的事物,

最佳答案

尝试这个

from django.utils.deconstruct import deconstructible

@deconstructible
class PathRename(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 = PathRename("/test")

然后
class ExcelUploadModel(models.Model):
document = models.FileField(upload_to=path_and_rename)

关于Django 无法进行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871248/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com