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 7904039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:53:40+00:00 2026-06-03T09:53:40+00:00

I have created a model which has foreign key to the django.contrib.auth.models User model.

  • 0

I have created a model which has foreign key to the django.contrib.auth.models User model. I need to retrieve the value the foreign key is referring to. But how to do that?

E.g. my model is

from django.contrib.auth.models import User
def FooModel(models.Model):
      user = models.ForeignKey(User)

then I know I can either use:

FooModel.objects.get() or FooModel.objects.filter() but as far as I know they will only return entries from the FooModel only. So how do I retrieve from the User model?

  • 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-03T09:53:41+00:00Added an answer on June 3, 2026 at 9:53 am
    m = FooModel.objects.get(id=123434)
    m.user.username
    

    You can use ..That wxample will return you the username of the related user. Also you can create joins using __ on the query filtering. Like:

    FooModel.objects.get(user__username='Foo')
    

    But, since you are building your query on FooModel, you will always get FooModel results. So you have you use . to use foreignkey relations to get to the required User field.

    EDIT:

    Django also allows you to use reverse relations on querysets, so even though User model do not have a foreignkey to FooModel on its model structure, you can use reverse relation of Foomodel.user foreignkey like:

    def FooModel(models.Model):
        user = models.ForeignKey(User)
        foofield = Models.CharField(...)
    
    
    
    User.objects.get(foomodel__foofield='Beatles')
    

    will return you the user which have FooModel record with a foreign key to his User record and foofield value of Beatles

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Django model called Person, which has got a 'user' ForeignKey
Currently I have a database object, vacancies which has a column (foreign key) CareerLevels.
I have created a blog app which its model has an author field like
I've got a question model and mcq choices model which have foreign key to
I have 2 models in my Django code: class ModelA(models.Model): name = models.CharField(max_length=255) description
I have a working model, but have noticed that the relationship has been created
I have a Product entity that has a foreign key relationship to Manufacturer. I
Hi have a table named category which has two foreign keys to the table
I have this model which has Image field to be uploaded. It has a
I have a model for an event simply which has start_date and end_date and

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.