在 django 和 playframework 中映射多对一
我需要为 django 和 Play! 中的应用程序建模客户和地址。我相信两个客户可以具有相同的地址。
那么在 django 中,客户和地址之间存在多对一关系
class Customer extends play.db.jpa.Model{
@ManyToOne
public Address address;
..
}
,下面的 python 代码是否给出了类似的映射?
class Address(models.Model):
customer= models.ForeignKey(Customer)
创建的表格会是什么样子?我在这里有点困惑..
I need to model a Customer and an Address for applications in django as well as in Play!.I believe that two Customers can have the same address.
So a Many to One relation between Customer and Address
class Customer extends play.db.jpa.Model{
@ManyToOne
public Address address;
..
}
In django ,does this python code below give similar mapping?
class Address(models.Model):
customer= models.ForeignKey(Customer)
What will be the tables created like?I am slightly confused here..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你几乎猜对了。 Django 中的多对一关系< /a> 确实由 models.ForeignKey 表示。
为了表达两个客户可以具有相同地址的关系,您可以在客户模型中定义该关系(而不是像您假设的那样在地址模型中)。
You almost got that right. The many-to-one relationship in Django is indeed represented by the models.ForeignKey.
To express relationship that two customers can have the same address you would define that relation in the Customer model (not in the Address model as you assumed).