diff --git a/apps/blog/migrations/0002_category_articlepage_category.py b/apps/blog/migrations/0002_category_articlepage_category.py index 75cd5ef..5cb13fa 100644 --- a/apps/blog/migrations/0002_category_articlepage_category.py +++ b/apps/blog/migrations/0002_category_articlepage_category.py @@ -68,7 +68,7 @@ class Migration(migrations.Migration): blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, - related_name="articles", + related_name="+", to="blog.category", ), ), @@ -79,7 +79,7 @@ class Migration(migrations.Migration): name="category", field=models.ForeignKey( on_delete=django.db.models.deletion.PROTECT, - related_name="articles", + related_name="+", to="blog.category", ), ), diff --git a/apps/blog/models.py b/apps/blog/models.py index 01a1567..5f27733 100644 --- a/apps/blog/models.py +++ b/apps/blog/models.py @@ -49,8 +49,9 @@ class HomePage(Page): id__in=ArticlePage.objects.live().public().values_list("tags__id", flat=True) ).distinct().order_by("name") ) - ctx["available_categories"] = ( - Category.objects.filter(show_in_nav=True, articles__live=True).distinct().order_by("sort_order", "name") + category_ids = ArticlePage.objects.live().public().values_list("category_id", flat=True) + ctx["available_categories"] = Category.objects.filter(show_in_nav=True, id__in=category_ids).order_by( + "sort_order", "name" ) return ctx @@ -76,9 +77,8 @@ class ArticleIndexPage(RoutablePageMixin, Page): tag_slug = request.GET.get("tag") articles = self.get_articles() all_articles = articles - available_categories = ( - Category.objects.filter(articles__in=all_articles).distinct().order_by("sort_order", "name") - ) + category_ids = all_articles.values_list("category_id", flat=True) + available_categories = Category.objects.filter(id__in=category_ids).order_by("sort_order", "name") category_links = [ {"category": category, "url": self.get_category_url(category)} for category in available_categories @@ -111,7 +111,8 @@ class ArticleIndexPage(RoutablePageMixin, Page): @route(r"^category/(?P[-\w]+)/$") def category_listing(self, request, category_slug): - category_qs = Category.objects.filter(articles__in=self.get_articles()).distinct() + category_ids = self.get_articles().values_list("category_id", flat=True) + category_qs = Category.objects.filter(id__in=category_ids) category = get_object_or_404(category_qs, slug=category_slug) return self.render(request, context_overrides=self.get_listing_context(request, active_category=category)) @@ -187,7 +188,7 @@ class TagMetadata(models.Model): class ArticlePage(SeoMixin, Page): - category = models.ForeignKey("blog.Category", on_delete=PROTECT, related_name="articles") + category = models.ForeignKey("blog.Category", on_delete=PROTECT, related_name="+") author = models.ForeignKey("authors.Author", on_delete=PROTECT) hero_image = models.ForeignKey( "wagtailimages.Image", null=True, blank=True, on_delete=SET_NULL, related_name="+"