有一个SQL问题,添加这个模型一切正常,问题出在ADMIN。
当我将数据添加到每个表中时,通过单击页面中的TYPE
&加载速度非常慢,安装和 SQL 需要 17 秒。当我尝试它给了我超时时,我的问题是我的模型有什么问题?是不是构造不好?PAGE
ADMIN
debug_toolbar
TYPE
PAGE
我的目标是让我们举个例子:
http://www.example.com/audi/4doors/s4/sport/red/audi-url
基本上所有 6 个 url 都是动态的,我将在每个表中指定,并且在其他表中也将在PAGE
as 下拉列表中。执行此操作或优化模型的最佳方法是什么?
这是 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)