【发布时间】:2014-05-02 17:09:56
【问题描述】:
将用户配置文件与同一类的对象相关联的最佳方法是什么。就我而言,父母双方都是用户配置文件类型的属性。 我的问题是我是否为每个父母创建一个字段,或者创建与用户配置文件的多对多关系,或者该关系应该与用户。 父亲和母亲都是系统的成员。
我的班级:
class UserProfile(models.Model):
user = models.OneToOneField(User)
father = models.ForeignKey(UserProfile)
mother = models.ForeignKey(UserProfile)
或:
class UserProfile(models.Model):
user = models.OneToOneField(User)
father = models.ForeignKey(User)
mother = models.ForeignKey(User)
或:
class UserProfile(models.Model):
user = models.OneToOneField(User)
parents = models.ManyToManyField(UserProfile)
或:
class UserProfile(models.Model):
user = models.OneToOneField(User)
parents = models.ManyToManyField(User)
什么是最好的方法?
【问题讨论】:
-
没有最好的方法,只是妥协,你应该基于你的特定用例。您是否希望能够处理例如?离婚再婚的父母用了
ManyToManyField,却以牺牲一些效率和易用性为代价?您以后可能想添加其他关系吗?您是否需要经常访问父母UserProfile,还是只需要访问User对象?如果没有对您要设计的系统有更详细的了解,这些都是无法回答的问题。 -
我的应用程序是一个教会管理系统,其中记录了教区居民的姓名和执行的圣礼。它记录了家庭的每个组成部分,父母和兄弟姐妹,他们也是系统的一部分,他们也有圣礼。这些教区居民也可能是传教士、秘书等。
标签: django python-2.7 django-models user-profile