Re: contrib.admin:list_editable - ForeignKey Performance is O(m*n)?

2010-06-29 Thread Jeremy Dunck
On Wed, Jun 30, 2010 at 2:04 AM, chadc  wrote:
...
> 3. Overriding the ForeignKey widget with a custom CachedSelect widget.
>
...
>
> PS: For the sake of posterity, I have included the code for my
> CachedSelect widget. Yes, I am aware that using regular expressions to
> parse the key is a nasty hack. If you have any better ideas, please
> let me know!

For clarity of what's going on here, would you mind posting an example model?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: contrib.admin:list_editable - ForeignKey Performance is O(m*n)?

2010-06-30 Thread Patryk Zawadzki
On Tue, Jun 29, 2010 at 6:04 PM, chadc  wrote:
> Hi there,
>
> I have been experiencing poor performance with django-admin when
> list_editable includes ForeignKey fields. In particular, rendering the
> changelist requires O(m*n) database queries where m is the number of
> ForeignKey fields included in list_editable and n is the number of
> rows in the changelist. I have searched extensively for possible
> causes and, after finding nothing and receiving no response on either
> django-users ('list_editable duplicate queries') or IRC, I am starting
> to think that it is a legitimate bug.

While this is worth to cache, you need to index the cache using a
serialized query, not the model.

It is possible to have a different set of choices for each model
instance (for example applying extra filter using another field's
value).

-- 
Patryk Zawadzki

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: contrib.admin:list_editable - ForeignKey Performance is O(m*n)?

2010-06-30 Thread chadc
Oh, yah, sure. Sorry.

class Host(models.Model):
name = models.CharField(max_length=128, unique=True)

class Account(models.Model):
host = models.ForeignKey(Host, related_name="accounts")
name = models.CharField(max_length=128)

class AccountAdmin(admin.ModelAdmin):
list_display = ('name', 'host')
list_editable = ('host',)
list_per_page = 25
admin.site.register(Account, AccountAdmin)

Rendering the FK widgets here requires n*m=25*1=25 database queries:

Form: SELECT "hosts_host"."id",  "hosts_host"."name" FROM "hosts_host"
ORDER BY "hosts_host"."name" ASC
Total time: 330 ms
Numer of queries: 25

Twenty five queries is, of course, not a big deal. However, the
default changelist is n=100, and with m=4 this becomes a real problem.

If you comment out the list_editable line or use the fix below
( formfield_overrides = {models.ForeignKey:{'widget':
CachedSelect()}} ), the number of queries returns to O(1).

The jist of what I am wondering is if the changelist is going to allow
editable ForeignKeys, should it be caching the choices rather than
hitting the database every time?



On Jun 29, 7:14 pm, Jeremy Dunck  wrote:
> On Wed, Jun 30, 2010 at 2:04 AM,chadc wrote:
>
> ...
>
> > 3. Overriding the ForeignKey widget with a custom CachedSelect widget.
>
> ...
>
> > PS: For the sake of posterity, I have included the code for my
> > CachedSelect widget. Yes, I am aware that using regular expressions to
> > parse the key is a nasty hack. If you have any better ideas, please
> > let me know!
>
> For clarity of what's going on here, would you mind posting an example model?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: contrib.admin:list_editable - ForeignKey Performance is O(m*n)?

2010-06-30 Thread Jeremy Dunck
On Thu, Jul 1, 2010 at 12:40 AM, chadc  wrote:
...
> The jist of what I am wondering is if the changelist is going to allow
> editable ForeignKeys, should it be caching the choices rather than
> hitting the database every time?

Yep, your models and explanation are clear.  I'm not sure that
CachedSelect is a good solution; it seems like there's a general
problem with repeated queries within ModelFormSet, not just admin--
it's just cropping up here.  I'll try to look at it by tomorrow.

File a ticket and let me know the number?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: contrib.admin:list_editable - ForeignKey Performance is O(m*n)?

2010-07-02 Thread chadc
Alright. I filed it as #13871 under contrib.admin because I do not
understand the larger issue, but please update it as you see fit.

Thanks, Jeremy.

Ticket Link: http://code.djangoproject.com/ticket/13871

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.