Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dev = [
"coverage[toml]",
"django-stubs",
"django-stubs-ext",
"django-twc-toolbox[cuid,history]",
"django-twc-toolbox[cuid,history,ninja]",
"faker",
"hatch",
"mypy",
Expand All @@ -63,6 +63,7 @@ docs = [
]
history = ["django-simple-history"]
lint = ["pre-commit"]
ninja = ["django-ninja"]

[project.urls]
Documentation = "https://django-twc-toolbox.westervelt.dev/"
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions src/django_twc_toolbox/mixins/ninja.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

from typing import Any # pyright: ignore[reportAny]
from typing import Generic
from typing import TypeVar
from typing import cast

from django.db.models import Model
from django.db.models import QuerySet
from ninja import ModelSchema

_T = TypeVar("_T", bound=ModelSchema)
_M = TypeVar("_M", bound=Model)


class QuerySetModelSchemaMixin(Generic[_T, _M]):
@classmethod
def from_queryset(cls: Any, queryset: QuerySet[_M]) -> list[_T]: # pyright: ignore[reportAny]
schema_cls = cast(type[_T], cls)
return [schema_cls.from_orm(obj) for obj in queryset.iterator()]