Django __unicode__和FK非常慢 [英] Django __unicode__ and FK is very slow

查看:87
本文介绍了Django __unicode__和FK非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写的东西像

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.

您有两个解决方案:


  1. 您可以覆盖 ProductInline queryset 方法包含 select_related('ChipStepping') (Django 1.3和上级)。或者,如果您想在其他地方修复问题,您可能需要更改 ComponentType 的默认管理器(对象 get_query_set 方法让它包含 select_related 调用。 / li>
  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.

这篇关于Django __unicode__和FK非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆