4

有一个SQL问题,添加这个模型一切正常,问题出在ADMIN。

当我将数据添加到每个表中时,通过单击页面中的TYPE&加载速度非常慢,安装和 SQL 需要 17 秒。当我尝试它给了我超时时,我的问题是我的模型有什么问题?是不是构造不好?PAGEADMINdebug_toolbarTYPEPAGE

我的目标是让我们举个例子:

http://www.example.com/audi/4doors/s4/sport/red/audi-url

基本上所有 6 个 url 都是动态的,我将在每个表中指定,并且在其他表中也将在PAGEas 下拉列表中。执行此操作或优化模型的最佳方法是什么?

这是 TYPE 页面加载的截图:

截图:http ://cl.ly/image/2931040E0t35

请帮忙谢谢

from django.db import models

class Client(models.Model):
    title = models.CharField(max_length=100, unique=True)
    def __unicode__(self):
        return self.title

class Category(models.Model):
    client = models.ForeignKey(Client, to_field='title')
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Subcategory(models.Model):
    client = models.ForeignKey(Client, to_field='title')
    category = models.ForeignKey(Category, to_field='title')
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Project(models.Model):
    client = models.ForeignKey(Client, to_field='title')
    category = models.ForeignKey(Category, to_field='title')
    subcategory = models.ForeignKey(Subcategory, to_field='title')
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Type(models.Model):
    client = models.ForeignKey(Client, to_field='title')
    category = models.ForeignKey(Category, to_field='title')
    subcategory = models.ForeignKey(Subcategory, to_field='title')
    project = models.ForeignKey(Project, to_field='title')
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Page(models.Model):
    client = models.ForeignKey(Client, to_field='title')
    category = models.ForeignKey(Category, to_field='title')
    subcategory = models.ForeignKey(Subcategory, to_field='title')
    project = models.ForeignKey(Project, to_field='title')
    type = models.ForeignKey(Type, to_field='title')
    pageurl = models.CharField(max_length=200)

admin.py当我从中删除外键时也刚刚发现list_display,它的工作速度非常快:

class ClientAdmin(admin.ModelAdmin):
    list_display = ('title',)
    admin.site.register(Client, ClientAdmin)

class CategoryAdmin(admin.ModelAdmin):
    list_display = ('client', 'title',)
    admin.site.register(Category, CategoryAdmin)

class SubcategoryAdmin(admin.ModelAdmin):
    list_display = ('client', 'category', 'title', )
    admin.site.register(Subcategory, SubcategoryAdmin)

class ProjectAdmin(admin.ModelAdmin):
    list_display = ('client', 'category', 'subcategory', 'title', )
    admin.site.register(Project, ProjectAdmin)

class TypeAdmin(admin.ModelAdmin):
    list_display = ('client', 'title', )
    admin.site.register(Type, TypeAdmin)

class PageAdmin(admin.ModelAdmin):
    list_display = ('client', )
    admin.site.register(Page, PageAdmin)

外键不能在 list_display 中?如何优化它们?

更新:

class Client(models.Model):
    title = models.CharField(max_length=100, unique=True, db_index=True)
    def __unicode__(self):
        return self.title

class Category(models.Model):
    client = models.ForeignKey(Client)
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Subcategory(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Project(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Type(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    project = models.ForeignKey(Project)
    title = models.CharField(max_length=200, unique=True)
    def __unicode__(self):
        return self.title

class Page(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    project = models.ForeignKey(Project)
    type = models.ForeignKey(Type)
    pageurl = models.CharField(max_length=200)

更新 2

from django.db import models


class Client(models.Model):
    title = models.CharField(max_length=100, primary_key=True)
    def __unicode__(self):
        return self.title

class Category(models.Model):
    client = models.ForeignKey(Client)
    title = models.CharField(max_length=200, primary_key=True)
    def __unicode__(self):
        return self.title

class Subcategory(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=200, primary_key=True)
    def __unicode__(self):
        return self.title

class Project(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    title = models.CharField(max_length=200, primary_key=True)
    def __unicode__(self):
        return self.title

class Type(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    project = models.ForeignKey(Project)
    title = models.CharField(max_length=200, primary_key=True)
    def __unicode__(self):
        return self.title

class Page(models.Model):
    client = models.ForeignKey(Client)
    category = models.ForeignKey(Category)
    subcategory = models.ForeignKey(Subcategory)
    project = models.ForeignKey(Project)
    type = models.ForeignKey(Type)
    pageurl = models.CharField(max_length=200)

更新 3 - ADMIN.PY

class ClientAdmin(admin.ModelAdmin):

    list_display = ('title',)

admin.site.register(Client, ClientAdmin)

class CategoryAdmin(admin.ModelAdmin):

    list_display = ('client', 'title',)

admin.site.register(Category, CategoryAdmin)

class SubcategoryAdmin(admin.ModelAdmin):

    list_display = ('client', 'category', 'title', )

admin.site.register(Subcategory, SubcategoryAdmin)

class ProjectAdmin(admin.ModelAdmin):

        list_display = ('client', 'category', 'subcategory', 'title', )

admin.site.register(Project, ProjectAdmin)

class TypeAdmin(admin.ModelAdmin):

    list_display = ('client', 'category', 'subcategory', 'project', 'title', )

admin.site.register(Type, TypeAdmin)

class PageAdmin(admin.ModelAdmin):

   list_display = ('client', 'category', 'subcategory', 'project', 'type', 'pageurl', )

admin.site.register(Page, PageAdmin)
4

2 回答 2

6

而不是你可以使用django admin 的raw_id_fields选项。

admin.site.register(Client)

class CategoryAdmin(admin.ModelAdmin):

    raw_id_fields = ('client',)

admin.site.register(Category, CategoryAdmin)

class SubcategoryAdmin(admin.ModelAdmin):

    raw_id_fields = ('client', 'category')

admin.site.register(Subcategory, SubcategoryAdmin)

class ProjectAdmin(admin.ModelAdmin):

    raw_id_fields = ('client', 'category', 'subcategory')

admin.site.register(Project, ProjectAdmin)

class TypeAdmin(admin.ModelAdmin):

    raw_id_fields = ('client', 'category', 'subcategory', 'project')

admin.site.register(Type, TypeAdmin)

class PageAdmin(admin.ModelAdmin):

    raw_id_fields = ('client', 'category', 'subcategory', 'project', 'type')

admin.site.register(Page, PageAdmin)

这是旧的 这是新的 sql 查询

于 2012-10-11T13:37:29.200 回答
2

嗯,我不确定这是否 100% 相关(因为我在 list_displaylist_editable 中使用了 FK)。我使用这里描述的技巧来加快速度:http: //blog.ionelmc.ro/2012/01/19/tweaks-for-making-django-admin-faster/

似乎对每一行的选择都进行了评估(尤其是在对 FK 使用 list_editable 时),因此使用 formfield_for_dbfield 您可以按照链接中的建议缓存选择。这节省了去数据库渲染foreign_keys的每个下拉/选择框。

如果您打开 mysql 常规日志(如果您使用的是 MySQL),您可以看到正在视图上执行的查询。

于 2012-11-08T22:42:11.370 回答