我正在记录一个处理对象存储,人员,事务和地址的Django项目
我希望你阅读这篇文章,就像对我的理智检查一样,但我真正的问题是在最后一段。
仅显示作为关系的字段
商店
# The intention here is for each Store to have exactly 1 address that can not be shared with another Store.
# I represent this on my diagram as 1-to-1 Store --> Address
address = models.OneToOneField(Address, on_delete=models.PROTECT)
人
# The intention here is for each Person (used for Employee and Customers) to have exactly 1 address that can not be shared with another Person.
# I represent this on my diagram as 1-to-1 Person --> Address
address = models.OneToOneField(Address, on_delete=models.PROTECT)
# The intention here is for many Persons to be associated with a Store.
# In this case the association would mean they were an employee of the Store.
# There is a dummpy Store that all customers are associated with (I know not the best design)
# I am represting this relationship as Many-to-1 Person --> Store
store = models.ForeignKey(Store, on_delete=models.PROTECT)
地址
# No relation fields
交易
# The intention here is for each Transaction exactly 1 Store that can not be shared with another Store.
# I represent this on my diagram as Many-to-1 Transaction --> Store
store = models.ForeignKey(Store, on_delete=models.PROTECT)
# Here is where it gets confusing to me. A Person(Customer) can have
# Many transactions with a Store but each Transaction only has 1 Person.
#So would this be a Many-to-1 or a Many-to-Many relation Transaction --> Person?
# The way it is defined currently works correctly. I am mostly asking about this conceptually.
person = models.ForeignKey(get_user_model(), on_delete=models.PROTECT)
该定义对事务 -> 个人关系有效。只是想另一种理解方式。当您使用 person_id 筛选事务模型时,您将获得属于该人员的记录(可能是多个(。因此,定义没有什么可改变的。在 django 文档中,您可以在多对一关系部分找到消息"要定义多对一关系,请使用外键"。