Download apps/core/models/querysets.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/core/models/querysets.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/core/models/querysets.py
-
curl -L -o querysets.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/core/models/querysets.py
2.19 kB
| """ | |
| QuerySet 预加载管理器 | |
| 为核心 Model 提供带预加载配置的查询集工厂方法, | |
| 消除各 Service 中重复的 select_related/prefetch_related 定义。 | |
| """ | |
| from __future__ import annotations | |
| from typing import TYPE_CHECKING, Any | |
| if TYPE_CHECKING: | |
| from django.db.models import QuerySet | |
| class CaseQuerySetManager: | |
| """案件查询集管理器""" | |
| SELECT_RELATED: tuple[str, ...] = ("contract",) | |
| PREFETCH_RELATED: tuple[str, ...] = ( | |
| "parties__client", | |
| "assignments__lawyer", | |
| "assignments__lawyer__law_firm", | |
| "logs__attachments", | |
| "case_numbers", | |
| "supervising_authorities", | |
| "contract__supplementary_agreements__parties__client", | |
| ) | |
| def with_standard_prefetch(cls) -> QuerySet[Any, Any]: | |
| """返回带标准预加载的查询集""" | |
| from django.apps import apps | |
| from django.db.models import QuerySet as QS | |
| Case = apps.get_model("cases", "Case") | |
| qs: QS[Any] = Case.objects.select_related(*cls.SELECT_RELATED).prefetch_related(*cls.PREFETCH_RELATED) | |
| return qs | |
| def with_extra_prefetch(cls, *extra: str) -> QuerySet[Any, Any]: | |
| """返回带额外预加载的查询集""" | |
| return cls.with_standard_prefetch().prefetch_related(*extra) | |
| class ContractQuerySetManager: | |
| """合同查询集管理器""" | |
| PREFETCH_RELATED: tuple[str, ...] = ( | |
| "cases", | |
| "contract_parties__client", | |
| "payments", | |
| "reminders", | |
| "assignments__lawyer", | |
| "assignments__lawyer__law_firm", | |
| ) | |
| def with_standard_prefetch(cls) -> QuerySet[Any, Any]: | |
| """返回带标准预加载的查询集""" | |
| from django.apps import apps | |
| from django.db.models import QuerySet as QS | |
| Contract = apps.get_model("contracts", "Contract") | |
| qs: QS[Any] = Contract.objects.prefetch_related(*cls.PREFETCH_RELATED) | |
| return qs | |
| def with_extra_prefetch(cls, *extra: str) -> QuerySet[Any, Any]: | |
| """返回带额外预加载的查询集""" | |
| return cls.with_standard_prefetch().prefetch_related(*extra) | |