对于具有特定关系的 Django 应用程序的数据库设计,我感到很困惑。我一直在查看以下数据库架构:http ://www.databaseanswers.org/data%5Fmodels/customers_and_orders/index.htm
我对 Customer_addresses 和客户以及地址之间的关系感到困惑。
我知道:
- 一个客户可以有多个地址。
- 许多客户可以拥有相同的(一个)地址
这是否等同于多对多关系?
- 很多客户可以有很多地址?
当我在 django 中构建模型时,我有(简化):
class Customer_Address(models.Model):
customer = models.ManyToManyField('inventory.Customer')
address = models.ManyToManyField('inventory.Address')
这是正确的吗?或者这更有意义:
class Customer_Address(models.Model):
customer = models.ForeignKeyField('inventory.Customer')
address = models.ManyToManyField('inventory.Address')
更新的问题:
基于它是多对多关系的想法。一个多对多关系是否需要两个多对多字段?
基于以下内容: http ://en.wikipedia.org/wiki/Many-to-many_%28data_model%29
由于 Django 支持 ManyToManyFields,我什至不需要联结表吗?如果我确实使用联结表(Customer_addresses),这篇文章似乎暗示使用两个 OneToMany 关系,这不就是像这样的两个外键吗?
class Customer_Address(models.Model):
customer = models.ForeignKeyField('inventory.Customer')
address = models.ForgeinKeyField('inventory.Address')
更新的问题:
现在我们知道 Django 构建了中间表,并且不需要 Customer_Addresses 表。哪个表应该有 ManyToManyfield?客户表?或地址表?
示例:https ://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
在 Person/Groups 示例中.. Groups 具有 ManyToManyField。是否有任何原因无法在 Person 表中定义 ManyToManyfield?