From cb0fa2da94973ddb8a7386a1664c79fd84ffbc94 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 3 Sep 2020 22:00:28 +0200 Subject: [PATCH] Definitively fix article field in comment admin form --- articles/admin.py | 5 ----- articles/context_processors.py | 2 +- articles/models.py | 4 ++-- articles/views/feeds.py | 2 +- articles/views/html.py | 7 ++----- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/articles/admin.py b/articles/admin.py index 134115d..7c36898 100644 --- a/articles/admin.py +++ b/articles/admin.py @@ -126,11 +126,6 @@ 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/context_processors.py b/articles/context_processors.py index 8470e66..0005973 100644 --- a/articles/context_processors.py +++ b/articles/context_processors.py @@ -6,7 +6,7 @@ def pages(request): def drafts_count(request): - return {"drafts_count": Article.with_pages.filter(status=Article.DRAFT).count()} + return {"drafts_count": Article.objects.filter(status=Article.DRAFT).count()} def date_format(request): diff --git a/articles/models.py b/articles/models.py index d3d1261..9657c58 100644 --- a/articles/models.py +++ b/articles/models.py @@ -50,8 +50,8 @@ class Article(AdminUrlMixin, models.Model): slug = models.SlugField(unique=True, max_length=255) comments_allowed = models.BooleanField(default=True) - objects = ArticleManager() - with_pages = models.Manager() + objects = models.Manager() + without_pages = ArticleManager() class Meta: ordering = ["-published_at"] diff --git a/articles/views/feeds.py b/articles/views/feeds.py index 93b1053..5c4e1fd 100644 --- a/articles/views/feeds.py +++ b/articles/views/feeds.py @@ -10,7 +10,7 @@ class CompleteFeed(Feed): description = settings.BLOG["description"] def items(self): - return Article.objects.filter(status=Article.PUBLISHED).order_by( + return Article.without_pages.filter(status=Article.PUBLISHED).order_by( "-published_at" )[:15] diff --git a/articles/views/html.py b/articles/views/html.py index cf8f848..a303957 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -14,21 +14,19 @@ class ArticlesListView(generic.ListView): model = Article paginate_by = 15 context_object_name = "articles" + queryset = Article.without_pages.filter(status=Article.PUBLISHED) def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) context["title"] = "Articles" return context - def get_queryset(self): - return super().get_queryset().filter(status=Article.PUBLISHED) - class DraftsListView(generic.ListView, LoginRequiredMixin): model = Article paginate_by = 15 context_object_name = "articles" - queryset = Article.with_pages.filter(status=Article.DRAFT) + queryset = Article.objects.filter(status=Article.DRAFT) def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) @@ -40,7 +38,6 @@ class ArticleDetailView(FormMixin, generic.DetailView): model = Article form_class = CommentForm context_object_name = "article" - queryset = Article.with_pages.all() template_name = "articles/article_detail.html" def get_queryset(self):