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

  • SEARCH
  • Home
  • 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 716215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:16:26+00:00 2026-05-14T05:16:26+00:00

The object user has a foreign key relationship to address. Is there a difference

  • 0

The object user has a foreign key relationship to address. Is there a difference between samples 1 and 2? Does sample 1 run the query multiple times? Or is the address object cached?

# Sample 1
country = user.address.country
city = user.address.city
state = user.address.state

# Sample 2
address = user.address
country = address.country
city = address.city
state = address.state
  • 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-05-14T05:16:27+00:00Added an answer on May 14, 2026 at 5:16 am

    The address object is indeed cached. You can see this if you print the contents of user.__dict__ before and after accessing user.address. For example:

    >>> user.__dict__
    {'date_joined': datetime.datetime(2010, 4, 1, 12, 31, 59),
     'email': u'user@test.com',
     'first_name': u'myfirstname',
     'id': 1L,
     'is_active': 1,
     'is_staff': 1,
     'is_superuser': 1,
     'last_login': datetime.datetime(2010, 4, 1, 12, 31, 59),
     'last_name': u'mylastname',
     'password': u'sha1$...$...',
     'username': u'myusername'}
    
    >>> country = user.address.country
    >>> user.__dict__
    {'_address': <myapp.models.address object at 0xwherever,
     'email': u'user@test.com',
     ...etc}
    

    So the user object gains a _address object which is used for subsequent lookups on the related object.

    You can use select_related() when you first get the user to pre-populate this cache even before accessing address, so you only hit the database once.

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

Sidebar

Related Questions

I have a User table which has a foreign key to the UserType table.
I've got the following situation: A User object has a Set of Permission objects
My User object that I want to create and store in the datastore has
Edit: I would like to model a 1 to 0:1 relationship between User and
Is it possible to return querysets that return only one object per foreign key?
I've got two objects: User and Permission, where User has a Collection. In my
I'm using the Facebook API and the class produces this object. object(User)#4 (5) {
If I have an object say @user and I want to render only certain
I have a function that loads a user object from a web service asynchronously.
I have an application where for each object the user can specify his own

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.