I have a performance problem when rendering a django template with a prefetch fetch request that is not very large (~ 1000 objects in the production environment / ~ 200 objects in the development environment), but has several levels of nesting.
Here is the prefetch:
stories = Story.objects.select_related( 'commande', 'commande__societe', 'commande__plateforme', 'client',).prefetch_related( Prefetch('crm_message_related', queryset=Message.objects.select_related( 'societe', 'plateforme', 'type_message').order_by('-date_received')), Prefetch('commande__crm_feedback_related'), Prefetch('commande__crm_guarantee_related'), Prefetch('commande__soyouz_item_related', queryset=Item.objects.select_related( 'etat', 'produit', 'produit__produit_enhanced', )), Prefetch('commande__tagged_items__tag'), Prefetch('commande__soyouz_item_related__tagged_items__tag'), Prefetch('commande__soyouz_item_related__soyouz_annulationclient_related'), Prefetch('commande__soyouz_item_related__soyouz_achat_related'), Prefetch('commande__soyouz_item_related__soyouz_achat_related__soyouz_receptionachat_related'), Prefetch('commande__soyouz_item_related__soyouz_achat_related__soyouz_annulationachat_related'), Prefetch('soyouz_action_related'), Prefetch('soyouz_action_related__receptionmessage__message', to_attr='receptionmessages', queryset=Message.objects.order_by( '-date_received').select_related( 'societe', 'type_message', 'plateforme')) ).order_by( '-commande__date_commande', '-date_created' ).all().distinct()
SQL runs smoothly, this is not a problem.
The problem is that I am trying to make my template very simple.
{% for story in stories %} {% with items=story.commande.soyouz_item_related.all %} {% for item in items %} {% for tag in item.tagged_items.all %} <button>{{ tag.tag }}</button> {% endfor %} {{ item.titre|safe|truncatechars:50 }} {% endfor %} {% endfor %}
It takes about 10 seconds to display a page in a production environment.
I profiled it in my development environment with django-template-timing ( https://github.com/orf/django-debug-toolbar-template-timings ) and really:
name Times Tot Time Queries Queries Time stories.html 1 2814,1 ms 0 0 ms
3 seconds for 3 cycles with 200 data (in development environment)? It seems a lot.
Any ideas on improving the speed of rendering templates?
Thanks a lot!