开发者

Django 1-1 relationship how to

开发者 https://www.devze.com 2023-02-12 22:02 出处:网络
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 patte
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

0

精彩评论

暂无评论...
验证码 换一张
取 消