Django 如何将用户配置文件与其他用户配置文件关联?
将用户配置文件与同一类的对象关联的最佳方式是什么。在我的例子中,两个父项都是User Profile类型的属性。 我的问题是,我是否为每个父项创建了一个字段,或者与用户配置文件创建了多个关系,或者该关系应该是与用户的关系。 父亲和母亲都是这个系统的成员 我的班级:Django 如何将用户配置文件与其他用户配置文件关联?,django,python-2.7,django-models,user-profile,Django,Python 2.7,Django Models,User Profile,将用户配置文件与同一类的对象关联的最佳方式是什么。在我的例子中,两个父项都是User Profile类型的属性。 我的问题是,我是否为每个父项创建了一个字段,或者与用户配置文件创建了多个关系,或者该关系应该是与用户的关系。 父亲和母亲都是这个系统的成员 我的班级: class UserProfile(models.Model): user = models.OneToOneField(User) father = models.ForeignKey(UserProfile)
class UserProfile(models.Model):
user = models.OneToOneField(User)
father = models.ForeignKey(UserProfile)
mother = models.ForeignKey(UserProfile)
或:
或:
或:
最好的方法是什么?我认为这会引起错误。您应该为所有三个模型字段添加相关的名称。那么处理关系的最佳方式是什么呢?我的意思是,从架构上来说,这是最好的方式。是的,您必须在最后两个字段中添加相关名称。这不是最好的方法,只是根据您的特定用例进行折衷。您是否希望能够处理离婚和再婚的父母与
ManyToManyField
,但要以一些效率和易用性为代价?以后是否要添加其他关系?您需要经常访问家长UserProfile
,还是只需要访问User
对象?如果你不了解你想要设计的系统,这些都是无法回答的问题。我的应用程序是一个教堂管理系统,其中记录了教区居民的姓名和执行的圣礼。它记录了每个家庭成员,父母和兄弟姐妹,他们也是系统的一部分,他们也有圣礼。这些教区居民也可能是教友、秘书等。
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)
class UserProfile(models.Model):
user = models.OneToOneField(User)
father = models.ForeignKey(User)
mother = models.ForeignKey(User)