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

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

问题描述

我的一个 Django 网站有以下数据库模型:在 Django 应用程序common"中:

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)

在 Django 应用情景喜剧"中:

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)

简而言之,模型类 Relation 有 3 个指向其他表的外键.问题是,当我使用 Django Admin 更改单个 Relation 时,页面 (change_form) 加载速度相当慢.后来,我将模型类 Relation 更改如下:

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)

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

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?

推荐答案

这不是 django Orm 的神奇之处.这是形式的魔力.当您在 Model 中创建外键时,然后在 ModelForm 中,会创建一个 ModelChoiceField,其中包含外键模型的所有选择.并且 django Admin 使用 Form 的所有属性来创建 HTML.所以使用这个代码.

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 

在招生网站

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天全站免登陆