fix(mypy): work around django-plugin annotate crash in comment viewset
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from typing import Any, cast
|
||||
|
||||
from django.db.models import Count, Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import ngettext
|
||||
@@ -49,22 +51,21 @@ class CommentViewSet(SnippetViewSet):
|
||||
add_to_admin_menu = True
|
||||
|
||||
def get_queryset(self, request):
|
||||
return (
|
||||
self.model.objects.all()
|
||||
.select_related("article", "parent")
|
||||
.annotate(
|
||||
pending_in_article=Count(
|
||||
"article__comments",
|
||||
filter=Q(article__comments__is_approved=False),
|
||||
distinct=True,
|
||||
)
|
||||
base_qs = self.model.objects.all().select_related("article", "parent")
|
||||
# mypy-django-plugin currently crashes on QuerySet.annotate() in this file.
|
||||
typed_qs = cast(Any, base_qs)
|
||||
return typed_qs.annotate(
|
||||
pending_in_article=Count(
|
||||
"article__comments",
|
||||
filter=Q(article__comments__is_approved=False),
|
||||
distinct=True,
|
||||
)
|
||||
)
|
||||
|
||||
def pending_in_article(self, obj):
|
||||
return obj.pending_in_article
|
||||
|
||||
pending_in_article.short_description = "Pending (article)"
|
||||
pending_in_article.short_description = "Pending (article)" # type: ignore[attr-defined]
|
||||
|
||||
|
||||
register_snippet(CommentViewSet)
|
||||
|
||||
Reference in New Issue
Block a user