如何在Django中表达一对多的关系 [英] How to express a One-To-Many relationship in Django

查看:193
本文介绍了如何在Django中表达一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义我的Django模型,我意识到模型字段类型中没有一个 OneToManyField 。我确定有办法做到这一点,所以我不知道我错过了什么。我基本上有这样的东西:

  class Dude(models.Model):
numbers = models.OneToManyField(' PhoneNumber')

class PhoneNumber(models.Model):
number = models.CharField()

在这种情况下,每个 Dude 可以有多个 PhoneNumber s,但关系应该是单向的,因为我不需要从 PhoneNumber 中知道, Dude 本身就拥有它,因为我可能有许多不同的对象拥有 PhoneNumber 实例,例如 Business ,例如:

  class Business(models.Model):
numbers = models.OneToManyField('PhoneNumber')

我将在模型中替换 OneToManyField (不存在)代表这种关系?我来自Hibernate / JPA,其中声明一对多关系一样简单:

  @OneToMany 
私人列表< PhoneNumber>电话号码;

我如何在Django中表达?

解决方案

要处理Django中的一对多关系,您需要使用 ForeignKey

关于ForeignKey的文档非常全面,应该回答所有的问题:



https://docs.djangoproject.com/en/dev/ref/模型/字段/#foreignkey



您的示例中的当前结构允许每个Dude拥有一个数字,每个数字属于多个Dudes(与商业)。



如果你想要反向关系,你需要添加两个ForeignKey字段到你的PhoneNumber模型,一个到Dude和一个到Business。这将允许每个数字属于一个Dude或一个Business,并且Dudes和Businesses能够拥有多个数字。我认为这可能是你以后的事情。

  class Business(models.Model):
...
class Dude(models.Model) :
...
class PhoneNumber(models.Model):
dude = models.ForeignKey(Dude)
business = models.ForeignKey(Business)


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:

class Dude(models.Model):
    numbers = models.OneToManyField('PhoneNumber')

class PhoneNumber(models.Model):
    number = models.CharField()

In this case, each Dude can have multiple PhoneNumbers, 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:

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:

@OneToMany
private List<PhoneNumber> phoneNumbers;

How can I express this in Django?

解决方案

To handle One-To-Many relationships in Django you need to use ForeignKey.

The documentation on ForeignKey is very comprehensive and should answer all the questions you have:

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).

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.

class Business(models.Model):
    ...
class Dude(models.Model):
    ...
class PhoneNumber(models.Model):
    dude = models.ForeignKey(Dude)
    business = models.ForeignKey(Business)

这篇关于如何在Django中表达一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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