[英]What field Django used in FOO_set?
I am little bit comfused. 我有点困惑。 Lets say I have such models.
可以说我有这样的模型。
models.py : models.py :
class Company(models.Model):
name = models.CharField(blank=False, null=False)
class Game(models.Model):
developer = models.ForeignKey(Company, on_delete=models.CASCADE)
publishers = models.ManyToManyField(Company)
If I use next code: 如果我使用下一个代码:
current_company = Company.object.get(pk=1)
current_company.game_set.all()
as I understand it return all games of current_company
, but what field (developer or publishers) Django used? 据我了解,它会返回
current_company
所有游戏,但是Django使用了哪个领域(开发人员或发行商)?
But this code wouldn't be valid, for precisely this reason. 但是正是出于这个原因,此代码无效。 If you tried to run it, Django would tell you that there was a conflict in the reverse relation.
如果您尝试运行它,Django会告诉您反向关系中存在冲突。
If you have two relationships pointing to the same model, you need to explicitly set related_name
on one of them to avoid this conflict. 如果您有两个指向同一模型的关系,则需要在其中之一上显式设置
related_name
以避免发生这种冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.