Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin change form load quite slow

Tags:

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?

like image 248
Brent81 Avatar asked May 26 '13 01:05

Brent81


People also ask

What is the Changelist in Django admin?

Django Admin's "change list" is the page that lists all objects of a given model. Now, all your articles should have a different name, and more explicit than "Article object".

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.


1 Answers

In admin.py

from django.contrib import admin  class RelationAdmin(admin.ModelAdmin):        raw_id_fields = ('Media','Particle',)  admin.site.register(Relation, RelationAdmin) 

This brings up a nice little UI element in the form and considerably improves performance since it doesn't have to load a huge number of options in the select box.

like image 139
Vishal Shah Avatar answered Sep 18 '22 19:09

Vishal Shah