profile = UserProfile.objects.get(....)
我尝试做的是获取当前登录用户的个人资料。我应该把什么放在括号里?
答案 0 :(得分:2)
Comon家伙,不需要那么苛刻......有些人实际上并不知道灰色轮廓复选标记是你应该按的。
UserProfile.objects.get(user=request.user)
但如果它是OneToOne
字段,您应该可以执行request.user.userprofile
http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationships
答案 1 :(得分:2)
假设您正在遵循此处描述的模式:
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
您应该可以使用以下内容:
def my_view(request):
user = request.user
if not isinstance(user, AnonymousUser):
profile = user.get_profile()
# do something with the profile here
else:
# handle anonymous users