Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango __unicode__ and FK is very slow
    text
    copied!<p>if I write something like</p> <pre><code>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") </code></pre> <p>And try to create an admin page for ComponentVendor</p> <pre><code>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) </code></pre> <p>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.</p> <p>This problem is reduced if I remove the foreign key references from the <strong>unicode</strong> functions of ChipStepping and ComponentType</p> <p>If there are enough entries in ComponentVendorProducts for a vendor I click on in the admin page, the page can take several minutes!</p> <p>Is there a way I can reduce the number of database hits on the admin page?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload