Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:41:04+00:00 2026-06-09T13:41:04+00:00

I have two models: User (pre-defined by Django) and UserProfile that are connected through

  • 0

I have two models: User (pre-defined by Django) and UserProfile that are connected through a foreign key.

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="connect")
    location = models.CharField(max_length=20, choices=LOCATION_CHOICES)
    gender = models.CharField(max_length=20, choices=GENDER_CHOICES)

The User model contains fields such as username, first_name, last_name, and password.

In my view, I want to find a UserProfile object using the username field which is located in the User Object. To filter/get a UserProfile object with the username field, I need to “travel” through the Foreign Key to access the User Model.

However, I got several errors in trying to do this. Does anyone know how I can do this?

This is what I have so far:

def edit(request):
    #the line below is where I am facing troubles. 
    #The error I'm getting is SyntaxError: keyword can't be an expression
    user = UserProfile.objects.filter(user.username=request.user.username)

    form1 = UserEditForm()
    form2 = UserProfileEditForm()
    c = RequestContext(request, {
        'action': 'update/',
        'button': 'Update', 
        'form1': form1,
        'form2': form2,
        })
    return render_to_response('registration/user_profile.html', c)

Does anyone know how to solve this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:41:05+00:00Added an answer on June 9, 2026 at 1:41 pm

    Use double underscores (__) to traverse relationships. E.g.:

    user = UserProfile.objects.filter(user__username=request.user.username)
    

    However, in your case, you don’t need to do this. request.user is a User object, so to get their UserProfile just do:

    user = request.user.get_profile()
    

    or even:

    UserProfile.objects.get(user=request.user)
    

    Note that your first example should return a QuerySet with 1 result, whereas the second returns a single object. In other words:

    request.user.get_profile() == 
        UserProfile.objects.filter(user__username=request.user.username)[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models in Django: User (pre-defined by Django) and UserProfile. The two
I have a User Model (pre-defined by Django) and a UserProfile model connected via
I have two models -- User and Entry -- that are related through a
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
If I have two Models that have a manytomany relationship with a through model,
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have two models class Friend(models.Model): me = models.ForeignKey(User) friend = models.ForeignKey(User) remark =
I have two models, Picture and SubmittedPicture as follows: class Picture(models.Model): user = models.ForeignKey(User)
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have two models User and Category that have a HABTM association. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.