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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:24:15+00:00 2026-05-29T14:24:15+00:00

if I write something like class Chip(models.Model): name = models.CharField(max_length=16) shortname = models.CharField(primary_key=True, unique=True,

  • 0

if I write something like

class Chip(models.Model):
  name      = models.CharField(max_length=16)
  shortname = models.CharField(primary_key=True, unique=True, max_length = 16)

  def __unicode__(self):
    return self.shortname

class ChipStepping(models.Model):

  stepping = models.CharField (max_length=16)
  ChipShortname = models.ForeignKey('Chip', db_column="ChipShortname")

  def __unicode__(self):
    return "%s:%s" % (self.ChipShortname, self.stepping)

class ComponentType(models.Model):
  name         = models.CharField (max_length=32)
  ChipStepping = models.ForeignKey('ChipStepping', db_column="ChipStepping")

  def __unicode__(self):
    return "%s(%s)" % (self.name, self.ChipStepping);

class ComponentVendor(models.Model):
  name     = models.CharField      (unique=True, max_length=16)
  products = models.ManyToManyField('ComponentType', through='ComponentVendorProduct', related_name='vendors')

  def __unicode__(self):
    return "%s" % (self.name)

class ComponentVendorProduct(models.Model):
  ComponentVendor = models.ForeignKey('ComponentVendor', db_column="ComponentVendor")
  ComponentType   = models.ForeignKey('ComponentType'  , db_column="ComponentType")

And try to create an admin page for ComponentVendor

class ProductInline(admin.TabularInline):
  model = ComponentVendor.products.through
  extra = 0

class ComponentVendorAdmin(admin.ModelAdmin):
  inlines = [ProductInline]
  list_filter = ['products__name']
  exclude = ['products']

admin.site.register(ComponentVendor, ComponentVendorAdmin)

The resulting page can take upwards of 30 sec. to load
From some debugging I’ve done, I found that it repeatedly makes redundant singular queries for ChipStepping and then Chip, with the same argument in the where clause instead of intelligently building a query that can lookup all the data.

This problem is reduced if I remove the foreign key references from the unicode functions of ChipStepping and ComponentType

If there are enough entries in ComponentVendorProducts for a vendor I click on in the admin page, the page can take several minutes!

Is there a way I can reduce the number of database hits on the admin page?

  • 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-29T14:24:17+00:00Added an answer on May 29, 2026 at 2:24 pm

    Your problem comes from the fact that Django is doing a DB call everytime you call __unicode__ on a ComponentType instance.

    You have two solutions to your issue:

    1. You override your ProductInline‘s queryset method to include select_related('ChipStepping') (Django 1.3 and superior).
    2. Alternatively, if you want to fix the issue elsewhere too, you might want to change your ComponentType‘s default manager (objects) get_query_set method to have it include the select_related call.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To set validation inside a Model in Rails, I should write something like: class
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self):
I'm trying to write something like: class MyClass(object): @staticmethod def test(x, y, z=None): if
I typically write something like this: class Person < ActiveRecord::Base attr_accessible :first_name, :last_name def
in jQuery you can write something like $(selector).bind('click', function(e) { // something was true
I want to write something like Skype, i.e. I have a constant audio stream
If I write something like this: var img = $(new Image()).attr('src', image.src); How can
In C# I'd write something like MyType arr = new MyType[10]; to alloc arr
Would you write something like: enum XYZ_TYPE {X=1, Y=2, Z=3}; I saw it and
I usually write something like this: <mx:VBox height=100% width=155> <mx:Button label=b1 width=100%/> <mx:Buttonlabel=b2 width=100%/>

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.