From e247f15797178e533525b7800ffd534400b79afe Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Sep 2020 21:15:59 +0200 Subject: [PATCH] Fix pages not displayed in comment admin form --- articles/admin.py | 5 +++++ articles/models.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/articles/admin.py b/articles/admin.py index e8fba8f..4ddac26 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -125,6 +125,11 @@ class CommentAdmin(admin.ModelAdmin): search_fields = ("username", "email", "content") actions = ["approve_comments", "reject_comments"] + def formfield_for_foreignkey(self, db_field, request, **kwargs): + if db_field.name == "article": + kwargs["queryset"] = Article.with_pages.all() + return super().formfield_for_foreignkey(db_field, request, **kwargs) + def approve_comments(self, request, queryset): count = queryset.update(status=Comment.APPROVED, user_notified=False) messages.success(request, f"Approved {count} message(s).") diff --git a/articles/models.py b/articles/models.py index 956321b..065eddf 100644 --- a/articles/models.py +++ b/articles/models.py @@ -56,7 +56,10 @@ class Article(AdminUrlMixin, models.Model): ordering = ["-published_at"] def __str__(self): - return self.title + type_ = "Article" + if hasattr(self, "page"): + type_ = "Page" + return f"{self.title} ({type_})" def get_admin_url(self): content_type = ContentType.objects.get_for_model(self.__class__)