Django:模型继承:FK &机对机

发布于 2024-09-28 12:43:13 字数 1141 浏览 8 评论 0原文

我该死地试图这样做: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-10-05 12:43:14

我解决了问题......

这会起作用:

from django.db import models

class CommonInfo(models.Model):

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

这会给你错误:AssertionError:ForeignKey无法定义与抽象类OtherModel的关系。这是因为您不能在指向另一个抽象类的抽象上建立 FK 关系或 M2M 关系。

from django.db import models

class CommonInfo(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

这对我来说是一个悲伤的消息。确实是令人悲伤的消息。我希望我的悲伤消息对其他人有益。

I figured out the problem.....

This will work:

from django.db import models

class CommonInfo(models.Model):

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

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.

from django.db import models

class CommonInfo(models.Model):
    class Meta:
        abstract = True

    name = models.CharField(max_length=100)

class Base(models.Model):
    m2m = models.ForeignKey(CommonInfo, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class ChildA(Base):
    pass

This is sad news for me. Sad news indeed. I hope my sad news benefits someone else.

探春 2024-10-05 12:43:13

您无法与抽象模型建立关系,但这正是您想要做的(ManyToManyFieldOtherModel)。如果您想完成这项工作,您需要从 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 to OtherModel). If you want to make this work you need to remove abstract = True from OtherModel and add common to your INSTALLED_APPS!

If you want to relate to different sublasses of OtherModel from the subclasses of Base you will need to define the relation on the subclass, not on the abstract model!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文