I'm defining my Django models right now and I realized that there wasn't a OneToManyField
in the model field types. I'm sure there's a way to do this, so I'm not sure what I'm missing. I essentially have something like this:
我现在正在定义我的Django模型,我意识到在模型字段类型中没有一个OneToManyField。我肯定有办法做到这一点,所以我不确定我错过了什么。本质上是这样的:
class Dude(models.Model):
numbers = models.OneToManyField('PhoneNumber')
class PhoneNumber(models.Model):
number = models.CharField()
In this case, each Dude
can have multiple PhoneNumber
s, but the relationship should be unidirectional, in that I don't need to know from the PhoneNumber
which Dude
owns it, per se, as I might have many different objects that own PhoneNumber
instances, such as a Business
for example:
在这种情况下,每个Dude可以有多个PhoneNumbers,但是关系应该是单向的,因为我不需要从PhoneNumber中知道哪个Dude拥有它,因为我可能有许多不同的对象,它们拥有PhoneNumber实例,比如业务:
class Business(models.Model):
numbers = models.OneToManyField('PhoneNumber')
What would I replace OneToManyField
(which doesn't exist) with in the model to represent this kind of relationship? I'm coming from Hibernate/JPA where declaring a one-to-many relationship was as easy as:
我用什么来代替模型中的OneToManyField(它不存在)来表示这种关系呢?我来自Hibernate/JPA,在那里声明一对多关系非常简单:
@OneToMany
private List<PhoneNumber> phoneNumbers;
How can I express this in Django?
我如何用Django来表示?
4 个解决方案
#1
72
To handle One-To-Many relationships in Django you need to use ForeignKey
.
要处理Django中的一对多关系,需要使用ForeignKey。
The documentation on ForeignKey is very comprehensive and should answer all the questions you have:
关于ForeignKey的文档非常全面,应该能回答你所有的问题:
https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
https://docs.djangoproject.com/en/dev/ref/models/fields/ foreignkey
The current structure in your example allows each Dude to have one number, and each number to belong to multiple Dudes (same with Business).
在您的示例中,当前的结构允许每个Dude具有一个数字,每个数字属于多个Dudes(与Business相同)。
If you want the reverse relationship, you would need to add two ForeignKey fields to your PhoneNumber model, one to Dude and one to Business. This would allow each number to belong to either one Dude or one Business, and have Dudes and Businesses able to own multiple Numbers. I think this might be what you are after.
如果你想要反向关系,你需要在你的PhoneNumber模型中添加两个外键字段,一个是Dude,一个是Business。这将使每个数字都属于一个人或一个企业,并且让人和企业能够拥有多个数字。我想这可能就是你想要的。
class Business(models.Model):
...
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
business = models.ForeignKey(Business)
#2
31
In Django, a one-to-many relationship is called ForeignKey. It only works in one direction, however, so rather than having a number
attribute of class Dude
you will need
在Django中,一对多关系称为ForeignKey。不过,它只在一个方向上工作,所以您需要的不是类Dude的数字属性
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
Many models can have a ForeignKey
to one other model, so it would be valid to have a second attribute of PhoneNumber
such that
许多模型可以有一个外键到另一个模型,所以有一个PhoneNumber的第二个属性是有效的。
class Business(models.Model):
...
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
business = models.ForeignKey(Business)
You can access the PhoneNumber
s for a Dude
object d
with d.phonenumber_set.objects.all()
, and then do similarly for a Business
object.
您可以使用d.phonenumber_set.objects.all()访问Dude对象d的PhoneNumbers,然后对业务对象进行类似的访问。
#3
9
You can use either foreign key on many side of OneToMany
relation (i.e. ManyToOne
relation) or use ManyToMany
(on any side) with unique constraint.
您可以在OneToMany关系(即many to one关系)的许多方面使用外键,也可以使用带有惟一约束的ManyToMany(任意方面)。
#4
0
To be more clear - there's no OneToMany in Django, only ManyToOne - which is Foreignkey described above. You can describe OneToMany relation using Foreignkey but that is very inexpressively.
更清楚的是,在Django中没有OneToMany,只有很多toone -这是上面描述的外国密钥。你可以用Foreignkey来描述一个关系,但那是非常含蓄的。
A good article about it: https://amir.rachum.com/blog/2013/06/15/a-case-for-a-onetomany-relationship-in-django/
关于它的一篇好文章:https://amir.rachum.com/blog/2013/06/15/a case for A -onetomany- relationships in django/
#1
72
To handle One-To-Many relationships in Django you need to use ForeignKey
.
要处理Django中的一对多关系,需要使用ForeignKey。
The documentation on ForeignKey is very comprehensive and should answer all the questions you have:
关于ForeignKey的文档非常全面,应该能回答你所有的问题:
https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
https://docs.djangoproject.com/en/dev/ref/models/fields/ foreignkey
The current structure in your example allows each Dude to have one number, and each number to belong to multiple Dudes (same with Business).
在您的示例中,当前的结构允许每个Dude具有一个数字,每个数字属于多个Dudes(与Business相同)。
If you want the reverse relationship, you would need to add two ForeignKey fields to your PhoneNumber model, one to Dude and one to Business. This would allow each number to belong to either one Dude or one Business, and have Dudes and Businesses able to own multiple Numbers. I think this might be what you are after.
如果你想要反向关系,你需要在你的PhoneNumber模型中添加两个外键字段,一个是Dude,一个是Business。这将使每个数字都属于一个人或一个企业,并且让人和企业能够拥有多个数字。我想这可能就是你想要的。
class Business(models.Model):
...
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
business = models.ForeignKey(Business)
#2
31
In Django, a one-to-many relationship is called ForeignKey. It only works in one direction, however, so rather than having a number
attribute of class Dude
you will need
在Django中,一对多关系称为ForeignKey。不过,它只在一个方向上工作,所以您需要的不是类Dude的数字属性
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
Many models can have a ForeignKey
to one other model, so it would be valid to have a second attribute of PhoneNumber
such that
许多模型可以有一个外键到另一个模型,所以有一个PhoneNumber的第二个属性是有效的。
class Business(models.Model):
...
class Dude(models.Model):
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
business = models.ForeignKey(Business)
You can access the PhoneNumber
s for a Dude
object d
with d.phonenumber_set.objects.all()
, and then do similarly for a Business
object.
您可以使用d.phonenumber_set.objects.all()访问Dude对象d的PhoneNumbers,然后对业务对象进行类似的访问。
#3
9
You can use either foreign key on many side of OneToMany
relation (i.e. ManyToOne
relation) or use ManyToMany
(on any side) with unique constraint.
您可以在OneToMany关系(即many to one关系)的许多方面使用外键,也可以使用带有惟一约束的ManyToMany(任意方面)。
#4
0
To be more clear - there's no OneToMany in Django, only ManyToOne - which is Foreignkey described above. You can describe OneToMany relation using Foreignkey but that is very inexpressively.
更清楚的是,在Django中没有OneToMany,只有很多toone -这是上面描述的外国密钥。你可以用Foreignkey来描述一个关系,但那是非常含蓄的。
A good article about it: https://amir.rachum.com/blog/2013/06/15/a-case-for-a-onetomany-relationship-in-django/
关于它的一篇好文章:https://amir.rachum.com/blog/2013/06/15/a case for A -onetomany- relationships in django/