The last example should read:

if db_field.name == 'baz':



Le jeudi 14 mai 2015 11:14:24 UTC-4, Simon Charette a écrit :
>
> It's a bit hard to tell without your model definition and the executed 
> queries but is it possible that the string representation method (__str__, 
> __unicode__) of one the models referenced by a M2M access another related 
> model during construction?
>
> e.g.
>
> from django.db import models
>
> class Foo(models.Model):
>     pass
>
> class Bar(models.Model):
>     foo = models.ForeignKey(Foo)
>
>     def __str__(self):
>         return "Foo %s" % self.foo
>
> class Baz(models.Model):
>     bars = models.ManyToManyField(Bar)
>
> Here the Bar model uses the Foo related model for string representation 
> construction and will result in N + 1 queries (N equals the number of Bar 
> objects) if the Baz.bars fields is displayed in the admin. To make sure 
> only one query is executed you must make sure Baz.bars are selected with 
> their related Foo objects:
>
> from django.contrib import admin
>
> class BazAdmin(admin.ModelAdmin):
>     def formfield_for_many_to_many(self, db_field, *args, **kwargs):
>         formfield = super(BazAdmin, self).formfield_for_many_to_many(
> db_field, *args, **kwargs)
>         if db_field.name == 'baz':
>             formfield.queryset = formfield.queryset.select_related('foo')
>         return formfield
>
> Simon
>
> Le jeudi 14 mai 2015 07:29:36 UTC-4, Timothy W. Cook a écrit :
>>
>> It isn't that the individual queries are very slow. But that there are so 
>> many of them. Attached is a screenshot of DjDT.
>> I see that I am not using a cache at all.  This was after doing a reload 
>> of the same standard Django admin/change_form.html. 
>>
>>
>> On Wed, May 13, 2015 at 1:58 PM, Tim Graham <timog...@gmail.com> wrote:
>>
>>> Are you sure it's the query that's slow and not the template rendering 
>>> and/or JavaScript performance?
>>>
>>>
>>> On Wednesday, May 13, 2015 at 12:32:50 PM UTC-4, Timothy W. Cook wrote:
>>>>
>>>> I have a model with 13 M2M relations and some of those have a few 
>>>> thousand instances. 
>>>> This renders rather slowly in the Admin.
>>>>
>>>> Thinking about improvements I wonder if it will help to setup 
>>>> prefetch_related queries
>>>>
>>>> https://docs.djangoproject.com/en/1.8/ref/models/querysets/#prefetch-related
>>>>
>>>> inside a  formfield_for_manytomany method?
>>>>
>>>>
>>>> https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany
>>>>
>>>> ​I haven't tried it yet and am not even sure how to go about it.  But 
>>>> if experienced developers think it will work, I'll give it a shot.
>>>>
>>>> Thoughts? ​
>>>>
>>>>
>>>> ============================================
>>>> Timothy Cook
>>>> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>>>> MLHIM http://www.mlhim.org
>>>>
>>>>   -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to django-users...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/58f721ad-2ee4-4016-ac6f-b48661c4ce5b%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/58f721ad-2ee4-4016-ac6f-b48661c4ce5b%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> -- 
>>
>> ============================================
>> Timothy Cook
>> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>> MLHIM http://www.mlhim.org
>>
>> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a98faea7-3973-44c3-bebb-d210a2e15cd9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to