WebFaction
Community site: login faq

Hello,

would like to ask, using Django Smart Selects for dynamic filtering dropdows in Admin:

https://github.com/digi604/django-smart-selects

My model looks like this:

from django.db import models
from smart_selects.db_fields import ChainedForeignKey

class Client(models.Model):
    title = models.CharField(max_length=100)
    def __unicode__(self):
        return self.title

class Category(models.Model):
    client = models.ForeignKey(Client)
    title = models.CharField(max_length=200)
    def __unicode__(self):
        return self.title

class Subcategory(models.Model):
    client = models.ForeignKey(Client)
    category = ChainedForeignKey(Category, chained_field="client", chained_model_field="client")
    title = models.CharField(max_length=200)
    def __unicode__(self):
        return self.title

class Project(models.Model):
    client = models.ForeignKey(Client)
    category = ChainedForeignKey(Category, chained_field="client", chained_model_field="client")
    subcategory = ChainedForeignKey(Subcategory, chained_field="category", chained_model_field="category")
    title = models.CharField(max_length=200)
    def __unicode__(self):
        return self.title

class Type(models.Model):
    client = models.ForeignKey(Client)
    category = ChainedForeignKey(Category, chained_field="client", chained_model_field="client")
    subcategory = ChainedForeignKey(Subcategory, chained_field="category", chained_model_field="category")
    project = ChainedForeignKey(Project, chained_field="subcategory", chained_model_field="subcategory")
    title = models.CharField(max_length=200)
    def __unicode__(self):
        return self.title

class Page(models.Model):
    client = models.ForeignKey(Client)
    category = ChainedForeignKey(Category, chained_field="client", chained_model_field="client")
    subcategory = ChainedForeignKey(Subcategory, chained_field="category", chained_model_field="category")
    project = ChainedForeignKey(Project, chained_field="subcategory", chained_model_field="subcategory")
    type = ChainedForeignKey(Type, chained_field="project", chained_model_field="project")
    pageurl = models.CharField(max_length=200, verbose_name="URL", unique=True)

My problem is when I am in Admin and filled out the data when I am clicking on each Table:

Client
Category
Subcategory
Project
Type
Page

Basically you should see:

image?admin - in case image it is not showing up it is here as a screenshot: http://cl.ly/image/2i3e2l0a1q0X

So when you clicking on all above all works except PAGE it takes like 5 minutes to load no sure why, I dont think the model is so that complicated to make heavy SQL or jquery processing.

Can anyone help or even maybe suggest better solution how to do Dynamic Admin filtering?

I can also open a ticket if possible to look fully at the code, thanks a lot

asked 23 Sep '12, 23:22

rentgeeen
4721319
accept rate: 0%

edited 26 Sep '12, 12:09

seanf
12.2k41836


We can not tell you why the app would be going slow from the code alone.

You will have to log your SQL queries and possibly attach a debugger to get statistics about the app and try to determine what it is actually processing.

With that info you will either have to rewrite the view to be more efficient or possibly write the author of the 'Django Smart Selects' application with your findings.

permanent link

answered 23 Sep '12, 23:59

johns
5.4k312
accept rate: 23%

I removed Smart Select plugin and did regular Foreign Keys: and when I add data and PRESS "TYPE" in admin it takes like 4 seconds to process SQL:

screenshot: http://cl.ly/image/3w3D3g0h3h1A

When I add PAGE model the last one it basically time out and when I look WF mysql server with TOP function in terminal its running on 300%, why that model is so bad? How should I make this entire model better that last PAGE has all prepopulated attributes from others?

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)

(24 Sep '12, 16:20) rentgeeen

I can open a ticket you will see how your sql server will go on 200-300% because of clicking in admin on type or page and that model is not huge

(24 Sep '12, 16:23) rentgeeen

We will want the SQL that is being generated, as stated above enable logging to get that. Also, we will not debug the application itself(to change what SQL it generates), you will have to contact the developer for that.

(24 Sep '12, 17:08) johns

This is the SQL:

SELECT `auto_type`.`id`, `auto_type`.`client_id`,
`auto_type`.`category_id`, `auto_type`.`subcategory_id`,
`auto_type`.`project_id`, `auto_type`.`title`, `auto_client`.`id`,
`auto_client`.`title`, `auto_category`.`id`, `auto_category`.`client_id`,
`auto_category`.`title`, T4.`id`, T4.`title`, `auto_subcategory`.`id`,
`auto_subcategory`.`client_id`, `auto_subcategory`.`category_id`,
`auto_subcategory`.`title`, T6.`id`, T6.`title`, T7.`id`, T7.`client_id`,
T7.`title`, T8.`id`, T8.`title`, `auto_project`.`id`,
`auto_project`.`client_id`, `auto_project`.`category_id`,
`auto_project`.`subcategory_id`, `auto_project`.`title`, T10.`id`,
T10.`title`, T11.`id`, T11.`client_id`, T11.`title`, T12.`id`,
T12.`title`, T13.`id`, T13.`client_id`, T13.`category_id`, T13.`title`,
T14.`id`, T14.`title`, T15.`id`, T15.`client_id`, T15.`title`, T16.`id`,
T16.`title` FROM `auto_type` INNER JOIN `auto_client` ON
(`auto_type`.`client_id` = `auto_client`.`title`) INNER JOIN
`auto_category` ON (`auto_type`.`category_id` = `auto_category`.`title`)
INNER JOIN `auto_client` T4 ON (`auto_category`.`client_id` = T4.`title`)
INNER JOIN `auto_subcategory` ON (`auto_type`.`subcategory_id` =
`auto_subcategory`.`title`) INNER JOIN `auto_client` T6 ON
(`auto_subcategory`.`client_id` = T6.`title`) INNER JOIN `auto_category`
T7 ON (`auto_subcategory`.`category_id` = T7.`title`) INNER JOIN
`auto_client` T8 ON (T7.`client_id` = T8.`title`) INNER JOIN
`auto_project` ON (`auto_type`.`project_id` = `auto_project`.`title`)
INNER JOIN `auto_client` T10 ON (`auto_project`.`client_id` = T10.`title`)
INNER JOIN `auto_category` T11 ON (`auto_project`.`category_id` =
T11.`title`) INNER JOIN `auto_client` T12 ON (T11.`client_id` =
T12.`title`) INNER JOIN `auto_subcategory` T13 ON
(`auto_project`.`subcategory_id` = T13.`title`) INNER JOIN `auto_client`
T14 ON (T13.`client_id` = T14.`title`) INNER JOIN `auto_category` T15 ON
(T13.`category_id` = T15.`title`) INNER JOIN `auto_client` T16 ON
(T15.`client_id` = T16.`title`) ORDER BY `auto_type`.`id` DESC
(24 Sep '12, 17:14) rentgeeen

here is also mysql query in PHPmyadmin:

http://cl.ly/image/2S320h3d0P0J

17 seconds

(24 Sep '12, 17:18) rentgeeen

Also just found out when I remove Foreign Keys from admin.py from "list_display", it works blazing fast:

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)

FOREIGN KEYS cannot be in list_display? How to optimize them?

(24 Sep '12, 18:29) rentgeeen

Now that you know the SQL You can profile it to get more info about why it is going slow. The way to optimize it is by rewriting the Django code to be more efficient, there is no simple answer for doing that, that is the core of what development work is.

(24 Sep '12, 19:39) johns

Did a mysql profiling, curious what is statistics that took so long:

Results:

Query:

http://cl.ly/image/0311392u0Z0S

CPU Profile:

http://cl.ly/image/2D210h0f1L06

(26 Sep '12, 09:17) rentgeeen

All of the various thread states are documented here: General Thread States

(26 Sep '12, 12:15) seanf
showing 5 of 9 show 4 more comments
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909
×40
×8
×7
×1

question asked: 23 Sep '12, 23:22

question was seen: 5,820 times

last updated: 26 Sep '12, 12:15

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM