Changeset View
Changeset View
Standalone View
Standalone View
comments/admin.py
| from typing import Optional | from typing import Optional, cast | ||||
| from django.contrib import admin | from django.contrib import admin | ||||
| from django.db.models import QuerySet | from django.db.models import QuerySet | ||||
| from django.db.models import Value, Case, When | from django.db.models import Value, Case, When | ||||
| from django.http.request import HttpRequest | from django.http.request import HttpRequest | ||||
| from comments import models | from comments import models | ||||
| from common.mixins import AdminUserDefaultMixin | from common.mixins import AdminUserDefaultMixin | ||||
| Show All 10 Lines | def get_queryset(self, request: HttpRequest) -> 'QuerySet[models.Comment]': | ||||
| When(section__isnull=False, then='section__name'), | When(section__isnull=False, then='section__name'), | ||||
| When(asset__isnull=False, then='asset__name'), | When(asset__isnull=False, then='asset__name'), | ||||
| default=Value(''), | default=Value(''), | ||||
| ) | ) | ||||
| ) | ) | ||||
| return queryset | return queryset | ||||
| def comment_under(self, obj: models.Comment) -> Optional[str]: | def comment_under(self, obj: models.Comment) -> Optional[str]: | ||||
| return obj._comment_under | return obj._comment_under # type: ignore | ||||
sybren: Why is it necessary to ignore the type?
Accessing a private property shouldn't happen. If it is… | |||||
natkaAuthorUnsubmitted Done Inline ActionsThis is a property added in the annotation above, in the get_queryset method. It is to be used only in the admin, to limit the number of database queries - I imagine that there will eventually be hundreds or thousands of comments and wanted to avoid sending a query or two for each of them. Since it's an annotation, mypy complains about Comment object not having such an attribute, and also does not recognise its type, raising two errors in this line (attr-defined and no-any-return). Do you have any suggestions on how to make mypy happy here? natka: This is a property added in the annotation above, in the `get_queryset` method. It is to be… | |||||
Why is it necessary to ignore the type?
Accessing a private property shouldn't happen. If it is important that this can be accessed from outside, make it so that it can be without suppressing warnings. If this really is the only way, explain why in a comment.