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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:30:35+00:00 2026-05-26T22:30:35+00:00

If I am retrieving an object with django, I can use .select_related() to instruct

  • 0

If I am retrieving an object with django, I can use .select_related() to instruct django to get all foreign key objects as well, to wit:

obj = ModelClass.objects.select_related().get(id=4) #1 db hit
foo = obj.long.chain.of.stuff #no db hit

If I already have obj, without it having been .select_related(), that is:

def doit(obj):
    obj.long.chain.of.stuff #4 db hits

is there any way to get django to fill in all of its foreign key relations? Something like:

def doit(obj):
    obj.magic() #1 db hit
    obj.long.chain.of.stuff #no db hits
  • 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-26T22:30:36+00:00Added an answer on May 26, 2026 at 10:30 pm

    I suppose I can do:

    def doit(obj):
        obj = obj.__class__.objects.select_related().get(id=obj.id) #1 db hit
        obj.long.chain.of.stuff #no db hits
    

    But is there any nicer way?

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

Sidebar

Related Questions

I'm retrieving a key from an object that looks like this: po obj {
I'm trying to retrieve a user object from a foreign key reference but each
I need help in retrieving a JSON object and parsing the array into my
When retrieving packages with Cabal, I frequently get errors with this message: user error
To extend the User object with custom fields, the Django docs recommend using UserProfiles
I am retrieving a WindowsIdentity object by calling: win_id = System.Security.Principal.WindowsIdentity.GetCurrent(); with the intention
I'm using an entity object called User. This is the function I use to
What is the best practice for retrieving an object from a collection within a
I have been working with NSMutableArray and have had no problems retrieving an object
I have an object stored in a database that I am retrieving in my

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.