mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
Auto-generate manager implementation for CustomSoftDelete
Besides requiring less code, this also allows the django-stubs mypy plugin to automatically generate a type-annotated version of the manager. Unfortunately, the plugin does not put a `ClassVar` annotation on `objects`, which is why we need the suppression.
This commit is contained in:
parent
a86c14e4e7
commit
68a4c14c74
2 changed files with 9 additions and 11 deletions
|
|
@ -1,13 +1,6 @@
|
|||
from model_utils.managers import SoftDeletableManager, SoftDeletableQuerySet
|
||||
from model_utils.managers import SoftDeletableQuerySet
|
||||
|
||||
|
||||
class CustomSoftDeleteQuerySet(SoftDeletableQuerySet):
|
||||
def only_read(self):
|
||||
return self.filter(is_read=True)
|
||||
|
||||
|
||||
class CustomSoftDeleteManager(SoftDeletableManager):
|
||||
_queryset_class = CustomSoftDeleteQuerySet
|
||||
|
||||
def only_read(self):
|
||||
return self.get_queryset().only_read()
|
||||
|
|
|
|||
|
|
@ -9,7 +9,12 @@ from django.utils.translation import gettext_lazy as _
|
|||
|
||||
from model_utils import Choices
|
||||
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
|
||||
from model_utils.managers import InheritanceManager, JoinManager, QueryManager
|
||||
from model_utils.managers import (
|
||||
InheritanceManager,
|
||||
JoinManager,
|
||||
QueryManager,
|
||||
SoftDeletableManager,
|
||||
)
|
||||
from model_utils.models import (
|
||||
SoftDeletableModel,
|
||||
StatusModel,
|
||||
|
|
@ -19,7 +24,7 @@ from model_utils.models import (
|
|||
)
|
||||
from model_utils.tracker import FieldTracker, ModelTracker
|
||||
from tests.fields import MutableField
|
||||
from tests.managers import CustomSoftDeleteManager
|
||||
from tests.managers import CustomSoftDeleteQuerySet
|
||||
|
||||
|
||||
class InheritanceManagerTestRelated(models.Model):
|
||||
|
|
@ -350,7 +355,7 @@ class SoftDeletable(SoftDeletableModel):
|
|||
class CustomSoftDelete(SoftDeletableModel):
|
||||
is_read = models.BooleanField(default=False)
|
||||
|
||||
objects: ClassVar[CustomSoftDeleteManager[SoftDeletableModel]] = CustomSoftDeleteManager()
|
||||
objects = SoftDeletableManager.from_queryset(CustomSoftDeleteQuerySet)() # type: ignore[misc]
|
||||
|
||||
|
||||
class StringyDescriptor:
|
||||
|
|
|
|||
Loading…
Reference in a new issue