覆盖子模型中的外键关系?
Override ForeignKey relationship in child model?
我正在尝试在 django 1.7 中做一些 model inheritance ,我想知道是否有办法覆盖外键字段?我有一个课程架构,我希望能够定义具有相同功能(页面、问题、级别等)的多个课程
这是我的代码:
http://hastebin.com/ixekepolik.py(第 117、118、120、121、123、129 和 131 行)
我目前遇到这个错误:
django.core.exceptions.FieldError: Local field 'level' in class 'HVPage' clashes with field of similar name from base class 'Page'
根据我的阅读here,目前不允许这样做(在 django 1.7 中)
Field name “hiding” is not permitted¶
In normal Python class inheritance, it is permissible for a child
class to override any attribute from the parent class. In Django, this
is not permitted for attributes that are Field instances (at least,
not at the moment). If a base class has a field called author, you
cannot create another model field called author in any class that
inherits from that base class.
Overriding fields in a parent model leads to difficulties in areas
such as initializing new instances (specifying which field is being
initialized in Model.init) and serialization. These are features
which normal Python class inheritance doesn’t have to deal with in
quite the same way, so the difference between Django model inheritance
and Python class inheritance isn’t arbitrary.
This restriction only applies to attributes which are Field instances.
Normal Python attributes can be overridden if you wish. It also only
applies to the name of the attribute as Python sees it: if you are
manually specifying the database column name, you can have the same
column name appearing in both a child and an ancestor model for
multi-table inheritance (they are columns in two different database
tables).
Django will raise a FieldError if you override any model field in any
ancestor model.
我正在尝试在 django 1.7 中做一些 model inheritance ,我想知道是否有办法覆盖外键字段?我有一个课程架构,我希望能够定义具有相同功能(页面、问题、级别等)的多个课程
这是我的代码:
http://hastebin.com/ixekepolik.py(第 117、118、120、121、123、129 和 131 行)
我目前遇到这个错误:
django.core.exceptions.FieldError: Local field 'level' in class 'HVPage' clashes with field of similar name from base class 'Page'
根据我的阅读here,目前不允许这样做(在 django 1.7 中)
Field name “hiding” is not permitted¶
In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this is not permitted for attributes that are Field instances (at least, not at the moment). If a base class has a field called author, you cannot create another model field called author in any class that inherits from that base class.
Overriding fields in a parent model leads to difficulties in areas such as initializing new instances (specifying which field is being initialized in Model.init) and serialization. These are features which normal Python class inheritance doesn’t have to deal with in quite the same way, so the difference between Django model inheritance and Python class inheritance isn’t arbitrary.
This restriction only applies to attributes which are Field instances. Normal Python attributes can be overridden if you wish. It also only applies to the name of the attribute as Python sees it: if you are manually specifying the database column name, you can have the same column name appearing in both a child and an ancestor model for multi-table inheritance (they are columns in two different database tables).
Django will raise a FieldError if you override any model field in any ancestor model.