gpt4 book ai didi

sql - 很多外键 - Django Admin

转载 作者:行者123 更新时间:2023-12-05 01:20:26 26 4
gpt4 key购买 nike

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

当我将数据添加到每个表中时,单击 TYPE & PAGEADMIN页面加载太慢,已安装 debug_toolbar SQL 用了 17 秒来处理 TYPE .当我尝试 PAGE 时它让我超时,我的问题是我的模型有什么问题?构造不好吗?

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

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

基本上所有 6 个 url 都是动态的,我会在每个表中指定,并且会在 PAGE 中。作为其他下拉菜单。这样做或优化模型的最佳方法是什么?

这是 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)

最佳答案

您可以使用 raw_id_fields 代替它django 管理员选项。

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)

this is the old one
this is new sql query

关于sql - 很多外键 - Django Admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12573659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com