Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix consecutive db queries #1364

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions liiweb/views/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ def get_context_data(self, **kwargs):
context["recent_legislation"] = Legislation.objects.filter(
metadata_json__stub=False
).order_by("-date")[:10]
context["taxonomies"] = Taxonomy.get_tree()
context["taxonomies"] = Taxonomy.get_root_nodes()
context["recent_articles"] = (
Article.objects.prefetch_related("topics")
Article.objects.prefetch_related("topics", "author")
.filter(published=True)
.select_related("author")
.order_by("-date")[:5]
)
return context
Expand Down
2 changes: 1 addition & 1 deletion peachjam/templates/peachjam/_taxonomy_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if taxonomies|length > 0 %}
<div class="row">
{% for taxonomy in taxonomies %}
{% if taxonomy.is_root and taxonomy.get_children|length > 0 %}
{% if taxonomy.is_root and taxonomy.get_children_count > 0 %}
<div class="col-12 col-lg-6 mb-4">
<div class="card">
<div class="card-header">{{ taxonomy }}</div>
Expand Down
35 changes: 22 additions & 13 deletions peachjam/views/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@

class ArticleListView(ListView):
model = Article
queryset = Article.objects.filter(published=True).order_by("-date")
queryset = (
Article.objects.filter(published=True)
.select_related("author")
.prefetch_related("topics")
.order_by("-date")
)
template_name = "peachjam/article_list.html"
context_object_name = "articles"
navbar_link = "articles"
paginate_by = 10

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
years = sorted(
list(
self.model.objects.filter(published=True)
.order_by()
.values_list("date__year", flat=True)
.distinct()
),
reverse=True,
years = (
self.model.objects.filter(published=True)
.dates("date", "year", order="DESC")
.values_list("date__year", flat=True)
.distinct()
)

context["years"] = [
Expand Down Expand Up @@ -69,6 +71,8 @@ def get_context_data(self, **kwargs):
)
context["more_articles"] = (
Article.objects.filter(author=self.object.author, published=True)
.select_related("author")
.prefetch_related("topics")
.exclude(pk=self.object.pk)
.order_by("-date")[:5]
)
Expand All @@ -92,7 +96,12 @@ def get_context_data(self, **kwargs):


class ArticleYearArchiveView(YearArchiveView):
queryset = Article.objects.filter(published=True).order_by("-date")
queryset = (
Article.objects.select_related("author")
.prefetch_related("topics")
.filter(published=True)
.order_by("-date")
)
date_field = "date"
make_object_list = True
allow_future = True
Expand All @@ -109,15 +118,15 @@ def get_queryset(self):

def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)

years = (
self.queryset.order_by("-date__year")
.dates("date", "year", order="DESC")
.values_list("date__year", flat=True)
.distinct()
)

context["years"] = [
{"url": reverse("article_year_archive", args=[year]), "year": year}
for year in years
{"url": reverse("article_year_archive", args=[y]), "year": y} for y in years
]

context["all_years_url"] = reverse("article_list")
Expand Down