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"
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue