Django 1-1 relationship how to

409 Views Asked by At
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?

2

There are 2 best solutions below

0
On BEST ANSWER

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
1
On

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