python-3.x - 抽象父模型中的 models.E006 - Django 3.1

标签 python-3.x django inheritance django-models

我有一个抽象模型和一些继承自它的其他类。

# models.py
class Parameter(models.Model):

    data = integer = models.IntegerField(blank=True, null=True)
    
    class Meta:
        abstract = True

class Temperature(Parameter):
    
    received_timestamp = models.DateTimeField(default=datetime.now, blank=True)

class Ph(Parameter):
    
    received_timestamp = models.DateTimeField(default=datetime.now, blank=True)

尽管我的 Parameter 类是抽象的,但我在 python manage.py makemigrations 脚本中收到 models.E006 错误。

graphs.Temperature.data: (models.E006) The field 'data' clashes with the field 'data' from model 'graphs.temperature'.

graphs.Ph.data: (models.E006) The field 'data' clashes with the field 'data' from model 'graphs.ph'.

基于Abstract base classes还有这个question ,如果我从抽象基类继承,则各个子类的字段名称与其父类不应该发生冲突(因为它是抽象的)。

有什么想法吗?

最佳答案

该错误与抽象基类无关。

问题在于定义IntegerField时的=integer,这意味着该字段被创建了两次。

更改:

data = integer = models.IntegerField(blank=True, null=True)`

data = models.IntegerField(blank=True, null=True)

关于python-3.x - 抽象父模型中的 models.E006 - Django 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65343715/

相关文章:

python - 在数据框行上运行以减少重复对 Python

python - django-admin:如果条件为真,则将列字体更改为红色

java - 我的 Java 中的 ShapeApp 出现问题

c++ - 强制调用基类虚函数

python - 如何在Python中解析和打印树

python - 在 Python 3.x 下将字符串传递给 ctypes 函数

python - Django 单元测试 - 客户端登录似乎不起作用

java - 重写 toString() 是否会导致与重写普通方法不同的行为?

python - 使用python用值替换文本文件中的单词

django - Elasticsearch过滤器(数字字段)不返回任何内容