Fewer db requests on home view.

- 1 request instead of (2 + #articles)
This commit is contained in:
Aurélien Delobelle 2017-04-06 19:44:16 +02:00
parent 2731d4630f
commit 026fba867d

View file

@ -55,11 +55,21 @@ class Home(TemplateView):
template_name = "kfet/home.html"
def get_context_data(self, **kwargs):
context = super(TemplateView, self).get_context_data(**kwargs)
articles = Article.objects.all().filter(is_sold=True, hidden=False)
context['pressions'] = articles.filter(category__name='Pression')
context['articles'] = (articles.exclude(category__name='Pression')
.order_by('category'))
context = super().get_context_data(**kwargs)
articles = list(
Article.objects
.filter(is_sold=True, hidden=False)
.select_related('category')
.order_by('category__name')
)
pressions, others = [], []
while len(articles) > 0:
article = articles.pop()
if article.category.name == 'Pression':
pressions.append(article)
else:
others.append(article)
context['pressions'], context['articles'] = pressions, others
return context
@method_decorator(login_required)