简体   繁体   English

来自两个不同模型的同一模型中的两个外键

[英]Two foreign key in the same model from two different model

I need to put two Foreign Keys in a model class from two different models. 我需要在两个不同模型的模型类中放置两个外键。 I would like to relate the third model class to the first and the second. 我想将第三个模型类与第一个和第二个模型类联系起来。

I've tried something like this: 我已经尝试过这样的事情:

class A (models.Model)
    id_A = models.IntergerField (primary_key=True)
    #...

    class B (models.Model)
     id_B = models.IntergerField (primary_key=True)
    #...

    class C (models.Model)
      id_A = models.ForeignKey(A)
      id_B = models.ForeignKey(B)

Reading the docs I understand that is not possible to have MultipleColumnPrimaryKeys ... but I Didn't receive any error from django with this models.py 阅读文档,我知道不可能有MultipleColumnPrimaryKeys ...但是我没有从django收到任何与此模型有关的错误。

Is it possible? 可能吗? Is there a smarter way? 有没有更聪明的方法?

Thank you! 谢谢!

您做得很好,如果将id_A和id_B关联到同一模型,django会给您一个错误,在这种情况下,只需将related_name属性放在第二个字段中即可。

You can use a many-to-many relationship that automatically creates that intermediate model for you: 您可以使用多对多关系为您自动创建该中间模型:

from django.db import models


class Publication(models.Model):
    title = models.CharField(max_length=30)


class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication, related_name='articles')

With this you can do both: 使用此方法,您既可以做到:

publication.articles.all()  # Gives you the articles of the current publication instance
article.publications.all()  # Gives you the publications of the current article instance

Check out docs for many to many 查看多对多文档

If you need to use any additional fields in that intermediate model, you can tell django which is the through model like this: 如果您需要在该中间模型中使用任何其他字段,则可以告诉django这样的through模型:

from django.db import models


class Person(models.Model):
    name = models.CharField(max_length=128)


class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')


class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

Check out docs for extra fields on many to many 查看文档, 了解多对多的额外字段

Also, if you are going for a standard integer auto-increment primary key, django already generates one for you so you don't need to define id_A and id_B . 另外,如果您要使用标准的整数自动增量主键,则django已经为您生成了一个主键,因此您无需定义id_Aid_B

Django didn't pop up error because you were doing it right. Django没有弹出错误,因为您做得对。 It's totally reasonable that there are multiple foreign keys in one model, just like class C. 像C类一样,在一个模型中有多个外键是完全合理的。

In class C, as long as id_A and id_B is the single column primary keys of their own model, it will perfectly work out. 在类C中,只要id_A和id_B是他们自己模型的单列主键,它就会完美地工作。

"MultipleColumnPrimaryKeys" you mentioned is a different thing. 您提到的“ MultipleColumnPrimaryKeys”是另一回事。 It means that for a specific table in database, there are multiple columns together to be the table's primary key, which is not supported in Django. 这意味着对于数据库中的特定表,将多个列加在一起作为该表的主键,而Django不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外键关联的两个不同模型的搜索结果 - Search result from two different model related by foreign-key 如何建立一个具有不同模型的两个外键关系的模型 - How to make a model having two foreign Key relations with the different models 带有两个外键字段指向同一个外模型的模型的 Django 聚合? - Django Aggregation with a Model that has two foreign key fields pointing to same foreign Model? 从一个Django模型迁移到使用外键引用的两个模型 - Migrate from one django model to two models referenced with a foreign key django ORM 在外键上加入两个 model - django ORM join two model on a foreign key Django - 如何连接两个具有不同键值的查询集(但来自同一模型) - Django - How to join two querysets with different key values (but from same model) 如何从同一模型序列化外键 - How to serialize a foreign key from the same model Django:当父模型有两个外键来自同一模型时,如何定义模型? - Django: How to define the models when parent model has two foreign keys come from one same model? 如何分别为同一个模型中的两个外键返回两个值? - How to return two values for two foreign keys in same model separately? 如何创建具有不同用户类型的帖子(一个帖子模型的两个外键)? - how to create post with different user type (two foreign key for one post model)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM