django - Django 3.0.9 错误消息 course.File.file: (models.E006) 字段 'file' 与模型 'courses.itembase' 中的字段 'file' 冲突
问题描述
我正在通过示例使用 Django 3 这本书来自学 Django 我已经通过这本书的方式工作并且遇到了错误消息
错误消息 course.File.file: (models.E006) 字段 'file' 与模型 'courses.itembase' 中的字段 'file' 冲突。和
course.Image.file:(models.E006)字段“file”与模型“courses.itembase”中的字段“file”冲突
我在这里使用了文档,我对“模型的字段”的理解是否正确。每个字段都被指定为一个类属性,每个属性都映射到一个数据库列。在我的情况下,“类文件”和“类图像”共享作为数据库列添加的属性“文件”。查看其他解决方案,我尝试重命名属性并提出请求
现在提供一次性默认值(将在所有现有行上设置此列的空值)
退出,让我在 models.py 中添加一个默认值
这是否表明数据库中有两列名为“文件”,我必须用唯一属性来标识它们。查看文档,我看到我已经正确使用了 Models.Filefield Here,您能否指导我查看有关如何在 models.py 中为该错误添加默认值的文档?
class ItemBase(models.Model):
owner = models.ForeignKey(User,
related_name='%(class)s_related',
on_delete=models.CASCADE)
title = models.CharField(max_length=250)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def render(self):
return render_to_string(
f'courses/content/{self._meta.model_name}.html',
{'item': self})
class Meta:
abstract = True
def __str__(self):
return self.title
class Text(ItemBase):
content = models.TextField()
class File(ItemBase):
file = models.FileField(upload_to='files')
class Image(ItemBase):
file = models.FileField(upload_to='images')
class Video(ItemBase):
url = models.URLField()
解决方案
你不能在 django 中的一个模型中设置相同的相关名称,所以你必须做的是你必须将你的相关名称更改为类似这样的名称,否则这不起作用然后你必须手动将其放入所有具有不同相关名称的类
owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s", on_delete=models.CASCADE)
推荐阅读
- php - 如何更改菜单的数组?
- docker - 在 docker-compose 中链接两个容器
- java - 我想在使用共享首选项生成的 ArrayList 中搜索输入字符串
- reactjs - Nodejs Api数据未保存到reactjs中的数组
- php - 请确认一个正则表达式以从字符串末尾剥离可选数字?
- java - 删除具有继承的实体不适用于弹簧数据
- ansible - 与 Stat 和 Command 模块调试语句相关的 Ansible 错误
- python - Pandas 使用循环复制行
- javascript - JS map 和 PHP 关联数组有什么区别吗?
- vue.js - 如何更改 vuejs 中标签标题上的图标?