Django管理员更改表单加载相当慢 [英] Django admin change form load quite slow

查看:235
本文介绍了Django管理员更改表单加载相当慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个Django网站有以下数据库模型:
在Django Appcommon:

  class Collection (models.Model):
name = models.CharField(max_length = 255,unique = True)
_short_name = models.CharField(db_column =short_name,max_length = 32,blank = True)

class Particle(models.Model):
content = models.TextField(blank = False)
owner = models.ForeignKey(Collection)
order = models.IntegerField null = True,blank = True

在Django Appsitcom中:

  class Media(models.Model):
name = models.CharField(max_length = 248)
_short_name = models.CharField db_column =short_name,max_length = 32,blank = True)
capital = models.CharField(max_length = 1)
description = models.TextField(blank = True)
progress = models。 CharField(max_length = 32,blank = True,null = True)

class关系(models.Model):
n ame = models.CharField(max_length = 128)
_short_name = models.CharField(db_column =short_name,max_length = 32,blank = True)
description = models.TextField(blank = True)
parent = models.ForeignKey('self',blank = True,null = True)
order = models.IntegerField(blank = True,null = True)
particle = models.ForeignKey(Particle,空白= True,null = True)
media = models.ForeignKey(Media,blank = True,null = True)

简而言之,模型类Relation有3个外键到其他表。
问题是,当我使用Django Admin更改单个关系时,页面(change_form)的载入速度相当缓慢。
以后,我改变了模型类的关系如下:

  class Relation(models.Model):
name = models.CharField(max_length = 128)
_short_name = models.CharField(db_column =short_name,max_length = 32,blank = True)
description = models.TextField(blank = True)
order = models.IntegerField(blank = True,null = True)
parent_id = models.IntegerField(blank = True,null = True)
particle_id = models.IntegerField(blank = True,null =真的)
media_id = models.IntegerField(blank = True,null = True)

修改将外键更改为IntegerFields,因此禁用了Django ORM系统中的一些魔法,现在更改表单页面加载速度非常快。
我的问题是,django orm中的残疾魔术是什么?有什么可能导致问题?

解决方案

这不是django Orm的魔力。这是Form的魔力。
当在Model中创建外键时,在ModelForm中,ModelChoiceField创建具有ForeignKey Model的所有选项。并且django Admin使用Form的所有属性来创建HTML。
所以使用这个代码。

 从django import forms 
class RelationForm(forms.ModelForm):
parent = forms.ChoiceField(required = False,
choices = Relation.objects.values_list('id','name'))
particle = forms.ChoiceField(required = False,
choices = Particle.objects.values_list('id','content'))
media = forms.ChoiceField(required = False,
choices = Media.objects.values_list('id','名字'))

class Meta:
model = Relation

在Admis站点

  from django.contrib import admin 
class RelationAdmin(admin.ModelAdmin):
form = RelationForm
model = Relation

您还可以缓存表单中的选择。


One of my Django websites has following database models: In Django App "common":

class Collection(models.Model):
    name = models.CharField(max_length = 255, unique = True)
    _short_name = models.CharField(db_column="short_name", max_length = 32, blank=True)

class Particle(models.Model):
    content = models.TextField(blank=False)
    owner = models.ForeignKey(Collection)
    order = models.IntegerField(null=True, blank=True)

In Django App "sitcom":

class Media(models.Model):
    name = models.CharField(max_length = 248)
    _short_name = models.CharField(db_column="short_name", max_length = 32, blank=True)
    capital = models.CharField(max_length = 1)
    description = models.TextField(blank=True)
    progress = models.CharField(max_length = 32, blank=True, null=True)

class Relation(models.Model):
    name = models.CharField(max_length = 128)
    _short_name = models.CharField(db_column="short_name", max_length = 32, blank=True)
    description = models.TextField(blank=True)
    parent = models.ForeignKey('self', blank=True, null=True)
    order = models.IntegerField(blank=True, null=True)
    particle = models.ForeignKey(Particle, blank=True, null=True)
    media = models.ForeignKey(Media, blank=True, null=True)

In short, model class Relation has 3 foreign keys to other tables. The problem is, when I use Django Admin to change a single Relation, the page (change_form) loads rather slowly. Later, I changed model class Relation as following:

class Relation(models.Model):
    name = models.CharField(max_length = 128)
    _short_name = models.CharField(db_column="short_name", max_length = 32, blank=True)
    description = models.TextField(blank=True)
    order = models.IntegerField(blank=True, null=True)
    parent_id = models.IntegerField(blank=True, null=True)
    particle_id = models.IntegerField(blank=True, null=True)
    media_id = models.IntegerField(blank=True, null=True)

The modification changed Foreign Keys to IntegerFields, so it disabled some of the magics inside Django ORM system, and now the change form page loads really fast. My question is, what is the "disabled magics inside django orm"? what has the potential to cause the problem?

解决方案

It is not the magic of django Orm. It is magic of Form. When you create a Foreign key in Model, then in ModelForm, a ModelChoiceField creates which has all choices of ForeignKey Model. And django Admin use all the properties of Form to create HTML. So use this code.

from django import forms
class RelationForm(forms.ModelForm):
    parent = forms.ChoiceField(required=False,
                              choices=Relation.objects.values_list('id', 'name'))
    particle = forms.ChoiceField(required=False,
                              choices=Particle.objects.values_list('id', 'content'))
    media = forms.ChoiceField(required=False,
                              choices=Media.objects.values_list('id', 'name'))

    class Meta:
        model = Relation 

In Admis Site

from django.contrib import admin
class RelationAdmin(admin.ModelAdmin):
    form = RelationForm
    model = Relation

You can also cache the choices pass in a form.

这篇关于Django管理员更改表单加载相当慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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