使用继承和外键时Django同步数据库冲突相关名称

发布于 2024-11-02 23:38:56 字数 1063 浏览 6 评论 0原文

这次我觉得不是我傻,而是真正的冲突。我有以下代码(简化):

from django.db import models

class Alpha(models.Model):
    relation = models.ForeignKey('Delta', related_name = 'reverse_relation', blank = True, null = True)

    class Meta:
        abstract = True

class Beta(Alpha):
    pass

class Gamma(Alpha):
    pass

class Delta(models.Model):
    pass

问题是 Delta.reverse_relation 可以引用 Beta 的实例或 Gamma 的实例。我必须以某种方式提供多个 related_name 值(或一个取决于类名的值)。我认为问题很清楚,但为了完整起见,错误(运行syncdb时): app.beta:字段“关系”的访问器与相关字段“Delta.reverse_relation”冲突。将 related_name 参数添加到“relation”的定义中。
app.beta:字段“relation”的反向查询名称与相关字段“Delta.reverse_relation”冲突。将 related_name 参数添加到“relation”的定义中。
app.gamma:字段“relation”的访问器与相关字段“Delta.reverse_relation”冲突。将 related_name 参数添加到“relation”的定义中。
app.gamma:字段“relation”的反向查询名称与相关字段“Delta.reverse_relation”冲突。将 related_name 参数添加到“relation”的定义中。

是否有可能将ForeignKey放置在父Alpha中,或者是将此代码剪切粘贴到Beta和Gamma的唯一方法?我宁愿不这样做,因为如果我无法在父级中定义所有子级共享的一半字段,那么它就会破坏继承点。

非常感谢任何帮助!

(如果有人可以评论为什么错误消息不在代码框中,我会解决这个问题。)

This time I think it's not me being stupid but an actual conflict. I have the below code (simplified):

from django.db import models

class Alpha(models.Model):
    relation = models.ForeignKey('Delta', related_name = 'reverse_relation', blank = True, null = True)

    class Meta:
        abstract = True

class Beta(Alpha):
    pass

class Gamma(Alpha):
    pass

class Delta(models.Model):
    pass

The problem is that Delta.reverse_relation could refer to an instance of Beta or an instance of Gamma. I would somehow have to provide multiple related_name values (or one that depends on the class name).I think the problem is clear but to be complete, the error (when running syncdb):
app.beta: Accessor for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.beta: Reverse query name for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.gamma: Accessor for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.
app.gamma: Reverse query name for field 'relation' clashes with related field 'Delta.reverse_relation'. Add a related_name argument to the definition for 'relation'.

Is it possible at all to place the ForeignKey in the parent Alpha, or is the only way to cut-paste this code to Beta and Gamma? I prefer not to do that because it kind of defeats the point of inheritance if I can't define in the parent half the fields that all children share.

Any help is much apprectiated!

(If anyone can comment with why the error messages aren't in a code box I'll fix that.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你不是我要的菜∠ 2024-11-09 23:38:56

我认为您会发现 Django 文档中的以下建议很有帮助且相关:
https://docs.djangoproject。 com/en/1.7/topics/db/models/#be-careful-with-lated-name

从本质上将关系字段的声明更改为:

relation = models.ForeignKey('Delta', related_name="%(app_label)s_%(class)s")

祝你好运......

I think that you will find the following advice in the Django documentation helpful and relevant:
https://docs.djangoproject.com/en/1.7/topics/db/models/#be-careful-with-related-name

Essentially change the declaration of the relation field to:

relation = models.ForeignKey('Delta', related_name="%(app_label)s_%(class)s")

Best of luck...

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