profile = UserProfile.objects.get(....)
what i try to do - is to 开发者_如何转开发get profile for the currently logged in user. What should i put in the brackets?
Assuming you are following the pattern described here:
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
You should be able to use the following:
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
Comon guys, no need to be so harsh... Some people actually don't know that the grey outline checkmark is what you're supposed to press.
UserProfile.objects.get(user=request.user)
But if it's a OneToOne
field, you should be able to do request.user.userprofile
http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationships
精彩评论