Django 项目文档关系类型

Django Project Documentation Type of Relationship

我正在记录处理对象 StorePersonTransaction 的 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 docs 中,您可以在多对一关系部分找到消息 "To define a many-to-one relationship, use ForeignKey"。