Django:模型继承:FK &机对机
我该死地试图这样做: http://docs.djangoproject。 com/en/dev/topics/db/models/#be-careful-with-lated-name
使用这种样式
这被保存为 common/abstract.py
class OtherModel(models.Model):
something = Charfield(max_length=100)
class Meta:
abstract = True
class Base(models.Model):
fk_model = models.ForeignKey(OtherModel, related_name="%(app_label)s_%(class)s_related")
class Meta:
abstract = True
然后我将其导入到另一个文件中。我们称之为 app/models.py
from common.abstract import Base
class ChildB(Base):
pass
我已经安装了“app”,但没有安装“common”。它会在没有 FK 或 M2M 关系的情况下很好地导入,但是当我尝试添加它时,我收到此错误:
/lib/python2.6/site-packages/django/db/models/fields/lated.py”,第 808 行,在初始化中 断言 not to.meta.abstract,“%s 无法定义与抽象类 %s 的关系” % (self.class._name__, to._meta.object_name) AssertionError:ForeignKey 无法定义与抽象类 OtherModel 的关系
请告知...如果您有任何问题或不理解我正在解释的内容,也请告诉我。我使用的文件非常复杂,所以我不想发布整个内容,因为我知道它会破坏这种关系。
I dam trying to do this:
http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name
Using this style
This is saved as common/abstract.py
class OtherModel(models.Model):
something = Charfield(max_length=100)
class Meta:
abstract = True
class Base(models.Model):
fk_model = models.ForeignKey(OtherModel, related_name="%(app_label)s_%(class)s_related")
class Meta:
abstract = True
Then I import it into another file. Let's call this app/models.py
from common.abstract import Base
class ChildB(Base):
pass
I have installed 'app' but not the 'common'. It will import nicely without the FK or M2M relationship, but when I try to add it, I get this error:
/lib/python2.6/site-packages/django/db/models/fields/related.py", line 808, in init
assert not to.meta.abstract, "%s cannot define a relation with abstract class %s" % (self.class._name__, to._meta.object_name)
AssertionError: ForeignKey cannot define a relation with abstract class OtherModel
Please advise.... also let me know if you have any questions or don't understand something that I am explaining. The file that I working with is really complex, so I didn't want to post the whole thing since I knew that it is breaking on this one relationship.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了问题......
这会起作用:
这会给你错误:AssertionError:ForeignKey无法定义与抽象类OtherModel的关系。这是因为您不能在指向另一个抽象类的抽象上建立 FK 关系或 M2M 关系。
这对我来说是一个悲伤的消息。确实是令人悲伤的消息。我希望我的悲伤消息对其他人有益。
I figured out the problem.....
This will work:
This will give you the error: AssertionError: ForeignKey cannot define a relation with abstract class OtherModel.This is because you can't have a FK relationship or M2M relationship on an abstract pointing to ANOTHER abstract class.
This is sad news for me. Sad news indeed. I hope my sad news benefits someone else.
您无法与抽象模型建立关系,但这正是您想要做的(
ManyToManyField
到OtherModel
)。如果您想完成这项工作,您需要从OtherModel
中删除abstract = True
并将common
添加到您的INSTALLED_APPS
!如果您想将
OtherModel
的不同子类与Base
的子类关联起来,您将需要在子类上定义关系,而不是在抽象模型上!You cannot have a relation with an abstract model, but this is what you are trying to do (
ManyToManyField
toOtherModel
). If you want to make this work you need to removeabstract = True
fromOtherModel
and addcommon
to yourINSTALLED_APPS
!If you want to relate to different sublasses of
OtherModel
from the subclasses ofBase
you will need to define the relation on the subclass, not on the abstract model!