自从 getProfile() 被删除后,如何查询用户配置文件中的数据?
how do I query data from a user profile since getProfile() is removed?
我的理解是,从 Django 1.5 开始,自定义用户模型是可能的,但不是必需的。事实上,Greenfeld 和 Roy 在 "Two Scoops of Django" 中争辩说,有时(例如创建第三方包)"Profile" 模型仍然是可行的方法。
但是,由于 getProfile() 已被删除,我不知道如何在模板中定位我的个人资料数据。因为:
{{ request.user.get_profile.id }}
不再产生任何数据,我试过了:
{{ request.user.userprofile.id }}
但这也不会产生任何值。
所以我的问题是,给定以下模型:
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True, null=True, on_delete=models.SET_NULL)
fullname = models.CharField(max_length=64, unique=False)
company = models.ForeignKey(ClientList, blank=False, null=True, db_column='client', on_delete=models.SET_NULL)
position = models.CharField(max_length=64, unique=False, blank=True, null=True)
egroup = models.CharField(max_length=50, choices=EMP_GROUP_CHOICES)
address1 = models.CharField(max_length=130, unique=False, blank=True, null=True)
address2 = models.CharField(max_length=30, unique=False, blank=True, null=True)
city = models.CharField(max_length=64, unique=False, blank=True, null=True)
state = models.CharField(max_length=64, unique=False, blank=True, null=True)
zipcode = models.CharField(max_length=15, unique=False, blank=True, null=True)
phone = models.CharField(max_length=15, unique=False, blank=True, null=True)
extension = models.CharField(max_length=15, unique=False, blank=True, null=True)
hphone = models.CharField(max_length=15, unique=False, blank=True, null=True)
mobile = models.CharField(max_length=15, unique=False, blank=True, null=True)
fax = models.CharField(max_length=15, unique=False, blank=True, null=True)
notes = models.TextField(max_length=2000, blank=True, null=True)
email = models.EmailField()
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
def __str__(self):
return u'%s' % self.fullname
class Meta:
ordering = ['fullname']
class Admin:
pass
如何获取模板中的 userprofile.id?
(注意:不是很相关,但用户对象仍然明确使用外键以保留 "on_delete" 参数)
要存储用户配置文件,建议使用OneToOneField
而不是ForeignKey
和unique=True
。虽然它们是等价的。
将关系设置为 OneToOneField
后,您可以使用以下查询检索用户配置文件 ID:user.userprofile.id
如果您决定将 ForeignKey
与 unique=True
一起使用,您仍然可以使用以下查询集检索它:user.userprofile_set.first()
参考:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
我的理解是,从 Django 1.5 开始,自定义用户模型是可能的,但不是必需的。事实上,Greenfeld 和 Roy 在 "Two Scoops of Django" 中争辩说,有时(例如创建第三方包)"Profile" 模型仍然是可行的方法。 但是,由于 getProfile() 已被删除,我不知道如何在模板中定位我的个人资料数据。因为:
{{ request.user.get_profile.id }}
不再产生任何数据,我试过了:
{{ request.user.userprofile.id }}
但这也不会产生任何值。
所以我的问题是,给定以下模型:
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True, null=True, on_delete=models.SET_NULL)
fullname = models.CharField(max_length=64, unique=False)
company = models.ForeignKey(ClientList, blank=False, null=True, db_column='client', on_delete=models.SET_NULL)
position = models.CharField(max_length=64, unique=False, blank=True, null=True)
egroup = models.CharField(max_length=50, choices=EMP_GROUP_CHOICES)
address1 = models.CharField(max_length=130, unique=False, blank=True, null=True)
address2 = models.CharField(max_length=30, unique=False, blank=True, null=True)
city = models.CharField(max_length=64, unique=False, blank=True, null=True)
state = models.CharField(max_length=64, unique=False, blank=True, null=True)
zipcode = models.CharField(max_length=15, unique=False, blank=True, null=True)
phone = models.CharField(max_length=15, unique=False, blank=True, null=True)
extension = models.CharField(max_length=15, unique=False, blank=True, null=True)
hphone = models.CharField(max_length=15, unique=False, blank=True, null=True)
mobile = models.CharField(max_length=15, unique=False, blank=True, null=True)
fax = models.CharField(max_length=15, unique=False, blank=True, null=True)
notes = models.TextField(max_length=2000, blank=True, null=True)
email = models.EmailField()
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
def __str__(self):
return u'%s' % self.fullname
class Meta:
ordering = ['fullname']
class Admin:
pass
如何获取模板中的 userprofile.id?
(注意:不是很相关,但用户对象仍然明确使用外键以保留 "on_delete" 参数)
要存储用户配置文件,建议使用OneToOneField
而不是ForeignKey
和unique=True
。虽然它们是等价的。
将关系设置为 OneToOneField
后,您可以使用以下查询检索用户配置文件 ID:user.userprofile.id
如果您决定将 ForeignKey
与 unique=True
一起使用,您仍然可以使用以下查询集检索它:user.userprofile_set.first()
参考:https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model