login community 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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:

1
2
3
4
5
6
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 Sep 23 '12 at 23:22

rentgeeen's gravatar image

rentgeeen
194

edited Sep 26 '12 at 12:09

seanf's gravatar image

seanf ♦♦
59891220


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.

answered Sep 23 '12 at 23:59

johns's gravatar image

johns ♦♦
345427

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)

(Sep 24 '12 at 16:20) rentgeeen rentgeeen's gravatar image

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

(Sep 24 '12 at 16:23) rentgeeen rentgeeen's gravatar image

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.

(Sep 24 '12 at 17:08) johns ♦♦ johns's gravatar image

This is the SQL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
(Sep 24 '12 at 17:14) rentgeeen rentgeeen's gravatar image

here is also mysql query in PHPmyadmin:

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

17 seconds

(Sep 24 '12 at 17:18) rentgeeen rentgeeen's gravatar image

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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?

(Sep 24 '12 at 18:29) rentgeeen rentgeeen's gravatar image

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.

(Sep 24 '12 at 19:39) johns ♦♦ johns's gravatar image

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

(Sep 26 '12 at 09:17) rentgeeen rentgeeen's gravatar image

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

(Sep 26 '12 at 12:15) seanf ♦♦ seanf's gravatar image
showing 5 of 9 show all
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
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

Tags:

×653
×34
×6
×4
×1

Asked: Sep 23 '12 at 23:22

Seen: 629 times

Last updated: Sep 26 '12 at 12:15

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited