Fewer db requests on home view.
- 1 request instead of (2 + #articles)
This commit is contained in:
parent
2731d4630f
commit
026fba867d
1 changed files with 15 additions and 5 deletions
|
@ -55,11 +55,21 @@ class Home(TemplateView):
|
||||||
template_name = "kfet/home.html"
|
template_name = "kfet/home.html"
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(TemplateView, self).get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
articles = Article.objects.all().filter(is_sold=True, hidden=False)
|
articles = list(
|
||||||
context['pressions'] = articles.filter(category__name='Pression')
|
Article.objects
|
||||||
context['articles'] = (articles.exclude(category__name='Pression')
|
.filter(is_sold=True, hidden=False)
|
||||||
.order_by('category'))
|
.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
|
return context
|
||||||
|
|
||||||
@method_decorator(login_required)
|
@method_decorator(login_required)
|
||||||
|
|
Loading…
Reference in a new issue