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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:58:17+00:00 2026-05-25T17:58:17+00:00

Context: Foo is a class that has a foreignKey field pointing to a Bar

  • 0

Context: Foo is a class that has a foreignKey field pointing to a Bar object. Several Foos may refer to the same Bar. (Many to one)

I need to reference several fields of a Foo object’s foreignkey (Bar.field and Bar.field2), but it seems that each time I do so it runs a separate query.

As a result, referencing these fields can take forever.

Is there a way to get all the Bar objects in a single query? Or is there a better way to speed it up?

foo_list = Foo.objects.filter(params) # takes 0.001 sec, returns 10k objects

result = []

for f in foo_list:

    # this part takes 40 seconds due to the huge number of queries 
    bar = f.bar # Foo contains a foreignKey reference to the Bar class

    result.append({
        "field": f.field,
        "barField": bar.barField,
        "barField2": bar.barField2,

        # this way was even worse
        # "barField": f.bar.barField,
        # "barField2": f.bar.barField2, 
    })
  • 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-25T17:58:17+00:00Added an answer on May 25, 2026 at 5:58 pm
    foo_list = Foo.objects.filter(params).select_related()
    

    does the job. The rest of the code would remain the same, but

    for f in foo_list:
        f.bar
    

    would use the cached objects

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

Sidebar

Related Questions

In the context of C++ (not that it matters): class Foo{ private: int x[100];
Imagine the following REBOL code: foo: context [bar: 3] I now have a context
Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar;
If I were to grab a field out of a database that has a
I've a UITableView which when a cell has the same content that other, this
I have a tomcat server with two webapps (foo and bar) that have an
Let's say my app has two models, Foo and Bar. Foo optionally belongs_to Bar.
I have a class that has dependencies that I've wired up with Ninject. public
I have a class Foo, which has a (simple) destructor. Some other class contains
I'm trying to build a jQuery.live like function. Helper is a class that has

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.