10👍
Based on the Manager[Any]
I assume you are already using django-stubs
.
Unfortunately it looks like there’s an open issue to make QuerySet.as_manager
generic over the model it’s attached to that has not been resolved yet.
Even if the PR addressing the issue got merged I’m afraid it wouldn’t address your immediate issue because the as_manager
needs to be generic over the generic QuerySet
subclass used to create the manager in order for both .active
to be available and attributes relating to Team
be available.
In this regard this other PR, which is unfortunately quite stale, seems to properly address your issue.
0👍
I’ve worked around this with a little switch-a-roo for MyPy’s sake:
_Q = TypeVar("_Q", bound="WorkflowQuerySet")
class WorkflowQuerySet(models.QuerySet["WorkflowModel"]):
"""
Queryset for workflow objects.
"""
def count_objects(self) -> int:
raise NotImplementedError
def latest_objects(self: _Q) -> _Q:
raise NotImplementedError
if TYPE_CHECKING:
# Create a type MyPy understands
class WorkflowManager(models.Manager["WorkflowModel"]):
def count_objects(self) -> int:
...
def latest_objects(self) -> _Q:
...
else:
WorkflowManager = WorkflowQuerySet.as_manager
class WorkflowModel(models.Model):
"""
A model that has workflow.
"""
objects = WorkflowManager()
- Rendering a ReportLab pdf built from SimpleDocTemplate
- Should I iterate on django query set or over the variable?
- Why does Django South 1.0 use iteritems()?
- Django: IntegrityError during Many To Many add()
- Django. Jquery. escaping string with quotes error
0👍
Here is my answer using generics and typevar
from typing import Generic, TypeVar
from django.db import models
class BookQueryset(models.QuerySet['Book']):
...
class Book(models.Model):
objects: BookQueryset = BookQueryset.as_manager()
book = Book.objects.all()[0]
If you inspect book is type Book
- Django 1.3 logging: 500 errors are not logged
- Django-mutant creating models in django-admin
- Django request.user.is_authenticated is always true?
- Daterange on a django-filter