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?