在两种型号的Django中都使用manytoman字段
我有两个模型,分别名为在两种型号的Django中都使用manytoman字段,django,django-models,Django,Django Models,我有两个模型,分别名为Profile和conference。我的要求是很多人可能会卷入一场争论,而一个人可能会有多场争论。话虽如此,我觉得我应该在两个模型中都使用ManyToMany字段,但我猜这违反了ManyToMany字段文档,因为它应该只在模型中使用 我的模型如下: class Profile(models.model): name = models.CharField(max_length=200) Controversy = models.ManyToManyField
Profile
和conference
。我的要求是很多人可能会卷入一场争论,而一个人可能会有多场争论。话虽如此,我觉得我应该在两个模型中都使用ManyToMany
字段,但我猜这违反了ManyToMany
字段文档,因为它应该只在模型中使用
我的模型如下:
class Profile(models.model):
name = models.CharField(max_length=200)
Controversy = models.ManyToManyField(Controversy) # As one person can have multiple controveries
class Controversy(models.Model):
year = models.Datefield()
other_people_involved = models.ManytoManyField(profile) # As multiple people can be involved in a controversy
description = models.TextField()
这显然会带来错误。
我无法理解如何处理这种情况您可以尝试以下方法:
创建另一个模型来存储个人
和争议
连接
class Profile(models.model):
name = models.CharField(max_length=200)
class Controversy(models.Model):
year = models.Datefield()
description = models.TextField()
class PeopleInvolved(models.Model):
controversy = models.ManyToManyField(Controversy)
person = models.ManytoManyField(profile)
因此,要列出一个人的争议,请执行以下操作:
controversies = [i.controversy for i in PeopleInvolved.objects.filter(person=[profile_id])] #pass the profile id of the person.
要列出与争议有关的人员,请执行以下操作:
peoples = [i.person for i in PeopleInvolved.objects.filter(controversy=[controversy_id])] #pass the controversy id.
您可以这样尝试:
创建另一个模型来存储个人
和争议
连接
class Profile(models.model):
name = models.CharField(max_length=200)
class Controversy(models.Model):
year = models.Datefield()
description = models.TextField()
class PeopleInvolved(models.Model):
controversy = models.ManyToManyField(Controversy)
person = models.ManytoManyField(profile)
因此,要列出一个人的争议,请执行以下操作:
controversies = [i.controversy for i in PeopleInvolved.objects.filter(person=[profile_id])] #pass the profile id of the person.
要列出与争议有关的人员,请执行以下操作:
peoples = [i.person for i in PeopleInvolved.objects.filter(controversy=[controversy_id])] #pass the controversy id.