【发布时间】:2011-03-14 09:35:07
【问题描述】:
在通过扩展 Django 用户模型创建 UserProfile 模型时,我发现关于是否使用 OneToOneField(User) 或 ForeignKey(User, unique=True) 的信息相互矛盾。
用这个更好吗?:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
还是这个?:
class UserProfile(models.Model):
user = models.OneToOneField(User)
Django Doc 指定OneToOneField,而Django Book example 使用ForeignKey。
James Bennett 也有两篇博客文章也提供了相互冲突的示例:
在上一篇文章中,Bennett 提供了他改用 ForeignKey 而不是 OneToOneField 的一些原因,但我不太明白,尤其是当我看到其他推荐相反的帖子时。
我很想知道您的偏好以及原因。或者,它甚至重要吗?
【问题讨论】:
-
我在 Bennett 的第一篇关于用户配置文件的文章中没有看到任何内容。
-
@Ignacio - 抱歉,我添加了指向错误文章的链接。应该是他关于“扩展用户模型”的帖子。我已经更正了链接。感谢您指出这一点。
-
对我来说,stackoverflow.com/questions/5870537/… 更清晰易懂
标签: django django-models django-users django-profiles