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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:40:47+00:00 2026-06-18T12:40:47+00:00

I have a model with a number of foreign key fields, e.g. model Product

  • 0

I have a model with a number of foreign key fields, e.g. model Product with fields ‘type’, ‘level’, ‘color’, ‘intensity’ (just a generic example).

I then have a page to edit all products of a given type using a Type form with the products as an inline formset with the option to add additional products inline using extra=10.

The thing i find very strange is that each time when i output one of the foreign key choice fields on the template Django queries the database for the get the options (every time).

For example:

{% for form in formset %}
    {{ form.level }}
    {{ form.color }}
    {{ form.intensity }}
{% endfor %}

With 20 products (and 10 empty extra forms) the above code issues 30 select * from ... from level, color and intensity totaling 90 queries (revealed using the Django Debug Toolbar), where 3 should be sufficient. The options are unlikely to change mid request, but even if they did I definitely wouldn’t want some newly added options to appear in the last 5 forms only.

Is there any way to optimize my models/forms/views/templates so that the database doesn’t get hammered like this unnecessarily?

–

Disclaimer: I’m relatively new to django and python and can’t help thinking there must be a way to address this built in somehow.

  • 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-18T12:40:48+00:00Added an answer on June 18, 2026 at 12:40 pm
    field_queryset = Test.objects.all()    
    for form in formset:
            form.fields['test_field'].queryset = field_queryset
    

    Like this.

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

Sidebar

Related Questions

I have a table which needs 2 fields. One will be a foreign key,
I have a .Net MVC4 model / view with a number of [Required] fields,
I have a Person model that has a foreign key relationship to Book ,
I have a model that has an arbitrary number of children entities. For simplicity
I have two arraylist with a number of model objects.I want to find the
I have a cakephp object (SecurityPerson) that has a few foreign key relationships to
I have a function that takes in a number of parameters.. it then does
Hay I have a model like this def Photo(models.Model): # Photo object fields... def
I want to populate two foreign key fields in one of my forms. The
I have models corresponding to database tables. For example, the House class has color,

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.