Did you modify your models before sending them out? You don't show any
foreign key relationships, but your queries indicate otherwise.

-James
On Nov 7, 2015 8:37 AM, "V1ce" <[email protected]> wrote:

> Hi,
>
> I got a lot of duplicated queries (in django debug toolbag) when i load my
> menu tabs , actually it's normal because i don't understand how to use
> database optimization for this, if possible i want some advice to make my
> reflexion better on this.
>
> Models(simplify) :
>
> class Categorie(models.Model):
>     nom = models.CharField(max_length=30)
>     slug = models.SlugField(max_length=100)
>     visible = models.BooleanField("Visible",default = False)
>
>     def getscateg(self):
>
>         return self.souscategorie_set.all().filter(visible = True)
>
>
> class SousCategorie(models.Model):
>     nom = models.CharField(max_length=30)
>     slug = models.SlugField(max_length=100)
>     visible = models.BooleanField("Visible",default = False)
>
>     def gettheme(self):
>
>         return self.theme_set.all().filter(visible = True)
>
>
> class Theme(models.Model):
>     nom = models.CharField(max_length=100)
>     slug = models.SlugField(max_length=100)
>     visible = models.BooleanField("Visible",default = False)
>
>     def getstheme(self): # Récupère les thèmes en fonction de la
> sous-categ traité
>
>         return self.soustheme_set.all().filter(visible=True)
>
>
> class SousTheme(models.Model):
>     nom = models.CharField(max_length=100)
>     slug = models.SlugField(max_length=100)
>     visible = models.BooleanField("Visible",default = False)
>
>
> views :
>
>
> def page(request):
>
>     categs = Categorie.objects.filter(visible=True)
>
>     return render(request, 'page.html', locals())
>
> templates (simplify) :
>
> {% for categ in categs %}
>     {% with currscat=categ.getscateg %}
>
>         <li class = "{% if currscat %} has_menu {% endif %}"> {{categ.nom}}  
> # This line hit database
>
>             {% if currscat %} # This line hit database
>
>                   <ul class = "menu-1">
>
>                    {% for souscateg in currscat %}
>                    {% with currth=souscateg.gettheme %}
>
>                        <li class="{%if currth%} has_menu {%endif%}"> 
> {{souscateg.nom}}  #This line hit database
>
>
>                            {% if currth %} # this line hit database
>
>
>                                <ul class = "menu-2">
>
>                                    ..........
>
>                                </ul>
>
>                            {% endif %}
>
>
> </li>
>
>
>                    {% endwith %}
>                    {% endfor %}
>                   </ul>
>
>             {% endif %}
>
>         </li>
>
>     {% endwith %}
>  {% endfor %}
>
>
> I think prefetch_related can help me here but i don't find to reduce
> resquest to database with it in this case... So after my template rendered
> i got like 50 request including 40 duplicate...
>
> i have try something like this in my views but without success, that just
> add more request to database  :
>
> categs =
> Categorie.objects.filter(visible=True).prefetch_related('souscategorie_set__theme_set__soustheme_set')
>
> Thanks for your help.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/2cb84db9-34ee-4dee-996d-4199a8eb8773%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2cb84db9-34ee-4dee-996d-4199a8eb8773%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CA%2Be%2BciXz%3DHEMNGpbZQQhVKd0DqBoTX8j2jWNgUC5ikCQKNzejg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to