Django __unicode__和FK非常慢 [英] Django __unicode__ and FK is very slow
问题描述
如果我写的东西像
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")
并尝试为ComponentVendor创建一个管理页面
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)
生成的页面可能需要30秒以上。加载
从我做的一些调试中,我发现它重复对ChipStepping然后再次对Chip进行冗余单个查询,在where子句中使用相同的参数,而不是智能构建可以查找所有数据的查询。
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.
如果我从ChipStepping和ComponentType
This problem is reduced if I remove the foreign key references from the unicode functions of ChipStepping and ComponentType
如果在供应商的ComponentVendorProducts中有足够的条目,我点击管理页面,页面可能需要几分钟!
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?
推荐答案
您的问题来自Django正在做一个DB在 ComponentType
实例中调用 __ unicode __
。
Your problem comes from the fact that Django is doing a DB call everytime you call __unicode__
on a ComponentType
instance.
您有两个解决方案:
- 您可以覆盖
ProductInline
的queryset
方法包含select_related('ChipStepping')
(Django 1.3和上级)。或者,如果您想在其他地方修复问题,您可能需要更改ComponentType
的默认管理器(对象
)get_query_set
方法让它包含select_related
调用。 / li>
- You override your
ProductInline
'squeryset
method to includeselect_related('ChipStepping')
(Django 1.3 and superior). - 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 theselect_related
call.
这篇关于Django __unicode__和FK非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!