【发布时间】:2015-08-17 21:53:55
【问题描述】:
在尝试对具有动态 upload_to 路径的 FileField 的模型使用迁移时,我遇到了以下错误。
field=models.FileField(null=True, upload_to=core.models.UserProfile.upload_path, blank=True),
AttributeError: type object 'UserProfile' has no attribute 'upload_path'
我的代码:
def upload_path(instance, filename):
return os.path.join(str(instance.user.id),filename)
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='userprofile')
document_upload = models.FileField(upload_to=upload_path,null=True, blank=True)
【问题讨论】:
-
删除迁移文件并再次运行
makemigrations。 -
由于
upload_path()函数不在一个类中,它不应该有第一个参数self,而是看起来像def upload_path(instance, filename)。您以前是否尝试过在课堂上使用它?错误消息还建议在那里而不是在core.models.upload_path中搜索。 -
@Leistungsabfall 没用。
-
@sthzg 是的,这是之前尝试的错误,但这不是问题
-
是这样想的。将
update_path拉到模块外部后,异常是否保持不变?因为它会从打印的内容中搜索upload_path内部的可调用UserProfile。
标签: python django django-models django-migrations