在一个字段中处理多个ForeinKeys _ Django [英] handle multiple ForeinKeys in one field _ Django

查看:35
本文介绍了在一个字段中处理多个ForeinKeys _ Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个模型A,它与模型B的关系为ForeinKey.

suppose we have a Model A that has relations ForeinKey to Model B.

class B(models.Model):
.....

class A(models.Model):
    foo = models.ForeinKey(B)

我如何拥有一个具有数十个 foo 的A模型的实例?为此我需要一个适当的解决方案.那可能吗?我认为,如果我们创建了许多foo字段,那就太好了.那么在django中有可能处理这种类型的问题吗?

how can I have an instance of A model that has dozens of foo ? and a proper solution I need for that. is that possible? I think this is dummm if we made many foo fields. so is it possible in django to handle this type of problems?

推荐答案

您的问题尚不清楚,但是我将尝试回答我想到的两种情况.

Your question is slightly unclear, but I will try to answer both cases that came to my mind.

例如,如果 B 代表人而 A 代表书,则可能是这种情况.现在,您需要具有例如以下字段:书籍作者,书籍出版者,书籍翻译者等.在这种情况下,您应该只创建多个外键.但是有一个陷阱... Django默认在您要引用的模型中创建虚拟字段,因此您可以遍历您的关系.该字段以类命名,您从中引用模型,在此示例中,其命名为 a_set .不幸的是,对于每个关系,该字段的名称都完全相同,因此它们会发生冲突.

This may be the case if for example B represents person and A represents book. And now you want to have for example fields for: author of the book, publisher of the book, translator of the book etc. In that case, you should just create multiple foreign keys. But there is a catch... Django is creating by default virtual field in model that you're referencing to, so you can traverse back your relation. This field is named after class, you're referencing the model from, and in this example it is named a_set. Unfortunately, for every relation, this field is named exactly the same, so they will clash.

该问题的解决方案是明确指定该虚拟字段的名称:

Solution for that issue is to either specify explicitly name of that virtual field:

class B(models.Model):
.....

class A(models.Model):
    author = models.ForeinKey(B, related_name='books_written')
    publisher = models.ForeinKey(B, related_name='books_published')
    translator = models.ForeinKey(B, related_name='books_translated')

或者您可以仅禁用这些反向字段:

or you can just disable those reverse fields:

class B(models.Model):
.....

class A(models.Model):
    author = models.ForeinKey(B, related_name='+')
    publisher = models.ForeinKey(B, related_name='+')
    translator = models.ForeinKey(B, related_name='+')

如果 B A 的每个分配都应具有(大致)相同的目的,或者您无法指定其中有多少.

例如,在以前的情况下,如果 A 代表书籍,而 B 代表人物,而您要做的就是将多个作者分配给一个作者,则可能会发生这种情况书.在这种情况下,您应该在这两个模型之间创建多对多关系.Django提供了 ManyToMany 字段,其用途非常简单:

If every assignment of B to A should have (roughly) the same purpose or you cannot specify how much of them will be there.

This can happen if for example, as in previous case, A represents book, and B represents person and all you want to do is to assign multiple authors to one book. In this case, you should create many-to-many relation between those two models. Django is offering ManyToMany field for that purpose and its usage is very simple:

class B(models.Model):
.....

class A(models.Model):
    authors = models.ManyToManyField(B)

如果出于任何原因您希望在这两个模型之间具有多个多对多关系,则与第一种情况一样,还会发生虚拟反向关系字段冲突的问题.修复该问题是等效的.

If for any reason you want to have multiple many-to-many relations between those two models, problem with clashing virtual reverse relation fields also occurs, as in first case. Fix for that is equivalent.

此外,您无需使用 ManyToManyField 来创建该关系,而是可以通过创建第三个模型来创建它们,而该模型将只具有它们的外键(这就是Django在后台执行的操作)当您使用该字段时).您甚至可以使用 ManyToManyField 使用该字段的 through 参数注册该模型,以具有直接关系和反向关系字段,以便于使用.

Also, instead of using ManyToManyField to create that relation, you can craft it by yourself, by creating 3rd model that will just have foreign keys to both of them (that's what Django is doing in background when you use that field). You can even register that model using ManyToManyField using through parameter of that field to have direct relation and reverse relation fields for ease of use.

这篇关于在一个字段中处理多个ForeinKeys _ Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆