Skip to content

Commit e9edf61

Browse files
committed
Remove more advanced imports from models/__init__.py
1 parent 22b798c commit e9edf61

File tree

9 files changed

+35
-112
lines changed

9 files changed

+35
-112
lines changed

plain-admin/plain/admin/cards/charts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any
33

44
from plain.admin.dates import DatetimeRangeAliases
5-
from plain.models import Count
5+
from plain.models.aggregates import Count
66
from plain.models.functions import (
77
TruncDate,
88
TruncMonth,

plain-flags/plain/flags/preflight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from plain.models import OperationalError, ProgrammingError
1+
from plain.models.db import OperationalError, ProgrammingError
22
from plain.preflight import PreflightCheck, PreflightResult, register_check
33
from plain.runtime import settings
44

plain-jobs/plain/jobs/admin.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
register_viewset,
1313
)
1414
from plain.http import ResponseRedirect
15+
from plain.models.expressions import Case, When
1516
from plain.runtime import settings
1617

1718
from .models import JobProcess, JobRequest, JobResult
@@ -200,13 +201,13 @@ class ListView(AdminModelListView):
200201
def get_initial_queryset(self) -> Any:
201202
queryset = super().get_initial_queryset()
202203
queryset = queryset.annotate(
203-
retried=models.Case(
204-
models.When(retry_job_request_uuid__isnull=False, then=True),
204+
retried=Case(
205+
When(retry_job_request_uuid__isnull=False, then=True),
205206
default=False,
206207
output_field=models.BooleanField(),
207208
),
208-
is_retry=models.Case(
209-
models.When(retry_attempt__gt=0, then=True),
209+
is_retry=Case(
210+
When(retry_attempt__gt=0, then=True),
210211
default=False,
211212
output_field=models.BooleanField(),
212213
),

plain-jobs/plain/jobs/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from plain import models
2626
from plain.models import transaction
27+
from plain.models.expressions import F
2728
from plain.runtime import settings
2829
from plain.utils import timezone
2930

@@ -410,7 +411,7 @@ def retryable(self) -> Self:
410411
return self.failed().filter(
411412
retry_job_request_uuid__isnull=True,
412413
retries__gt=0,
413-
retry_attempt__lt=models.F("retries"),
414+
retry_attempt__lt=F("retries"),
414415
)
415416

416417
def retry_failed_jobs(self) -> None:

plain-models/plain/models/__init__.py

Lines changed: 20 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,14 @@
1+
from .registry import models_registry, register_model # noqa Create the registry first
12
from . import (
2-
preflight, # noqa
3-
)
4-
from .aggregates import (
5-
Aggregate,
6-
Avg,
7-
Count,
8-
Max,
9-
Min,
10-
StdDev,
11-
Sum,
12-
Variance,
13-
)
14-
from .constraints import (
15-
BaseConstraint,
16-
CheckConstraint,
17-
Deferrable,
18-
UniqueConstraint,
19-
)
20-
from .db import (
21-
DatabaseError,
22-
DataError,
23-
Error,
24-
IntegrityError,
25-
InterfaceError,
26-
InternalError,
27-
NotSupportedError,
28-
OperationalError,
29-
ProgrammingError,
30-
db_connection,
31-
)
32-
from .deletion import (
33-
CASCADE,
34-
DO_NOTHING,
35-
PROTECT,
36-
RESTRICT,
37-
SET,
38-
SET_DEFAULT,
39-
SET_NULL,
40-
ProtectedError,
41-
RestrictedError,
42-
)
43-
from .enums import Choices, IntegerChoices, TextChoices
44-
from .expressions import (
45-
Case,
46-
Exists,
47-
F,
48-
OuterRef,
49-
Subquery,
50-
Value,
51-
When,
52-
Window,
3+
preflight, # noqa Imported for side effects (registers preflight checks)
534
)
5+
6+
# Imports that would create circular imports if sorted
7+
from .base import Model
8+
from .constraints import CheckConstraint, UniqueConstraint
9+
from .db import IntegrityError, db_connection
10+
from .deletion import CASCADE, DO_NOTHING, PROTECT, RESTRICT, SET, SET_DEFAULT, SET_NULL
11+
from .enums import IntegerChoices, TextChoices
5412
from .fields import (
5513
BigIntegerField,
5614
BinaryField,
@@ -75,37 +33,24 @@
7533
UUIDField,
7634
)
7735
from .fields.json import JSONField
78-
from .indexes import Index
79-
from .query import Prefetch, QuerySet, prefetch_related_objects
80-
from .query_utils import FilteredRelation, Q
81-
from .registry import models_registry, register_model
82-
83-
# Imports that would create circular imports if sorted
84-
from .base import Model # isort:skip
85-
from .options import Options # isort:skip
86-
from .fields.related import ( # isort:skip
36+
from .fields.related import (
8737
ForeignKey,
8838
ManyToManyField,
8939
)
40+
from .indexes import Index
41+
from .options import Options
42+
from .query import QuerySet
43+
from .query_utils import Q
9044

91-
45+
# This module exports the user-facing API for defining model classes,
46+
# with a secondary focus on the most common query utilities like Q.
47+
# Advanced query-time features (aggregates, expressions, etc.) should be
48+
# imported from their specific modules (e.g., plain.models.aggregates).
9249
__all__ = [
93-
# From aggregates
94-
"Aggregate",
95-
"Avg",
96-
"Count",
97-
"Max",
98-
"Min",
99-
"StdDev",
100-
"Sum",
101-
"Variance",
10250
# From constraints
103-
"BaseConstraint",
10451
"CheckConstraint",
105-
"Deferrable",
10652
"UniqueConstraint",
10753
# From enums
108-
"Choices",
10954
"IntegerChoices",
11055
"TextChoices",
11156
# From fields
@@ -130,6 +75,8 @@
13075
"TimeField",
13176
"URLField",
13277
"UUIDField",
78+
# From fields.json
79+
"JSONField",
13380
# From indexes
13481
"Index",
13582
# From deletion
@@ -140,27 +87,11 @@
14087
"SET",
14188
"SET_DEFAULT",
14289
"SET_NULL",
143-
"ProtectedError",
144-
"RestrictedError",
145-
# From expressions
146-
"Case",
147-
"Exists",
148-
"F",
149-
"OuterRef",
150-
"Subquery",
151-
"Value",
152-
"When",
153-
"Window",
154-
# From fields.json
155-
"JSONField",
15690
# From options
15791
"Options",
15892
# From query
159-
"Prefetch",
16093
"QuerySet",
161-
"prefetch_related_objects",
16294
# From query_utils
163-
"FilteredRelation",
16495
"Q",
16596
# From base
16697
"Model",
@@ -169,15 +100,7 @@
169100
"ManyToManyField",
170101
# From db
171102
"db_connection",
172-
"DatabaseError",
173103
"IntegrityError",
174-
"InternalError",
175-
"ProgrammingError",
176-
"DataError",
177-
"NotSupportedError",
178-
"Error",
179-
"InterfaceError",
180-
"OperationalError",
181104
# From registry
182105
"register_model",
183106
"models_registry",

plain-models/plain/models/backends/sqlite3/operations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import TYPE_CHECKING, Any
99

1010
from plain import models
11+
from plain.models.aggregates import Aggregate, Avg, StdDev, Sum, Variance
1112
from plain.models.backends.base.operations import BaseDatabaseOperations
1213
from plain.models.constants import OnConflict
1314
from plain.models.db import DatabaseError, NotSupportedError
@@ -48,7 +49,7 @@ def bulk_batch_size(self, fields: list[Any], objs: list[Any]) -> int:
4849

4950
def check_expression_support(self, expression: Any) -> None:
5051
bad_fields = (models.DateField, models.DateTimeField, models.TimeField)
51-
bad_aggregates = (models.Sum, models.Avg, models.Variance, models.StdDev)
52+
bad_aggregates = (Sum, Avg, Variance, StdDev)
5253
if isinstance(expression, bad_aggregates):
5354
for expr in expression.get_source_expressions():
5455
try:
@@ -65,7 +66,7 @@ def check_expression_support(self, expression: Any) -> None:
6566
"since date/time is saved as text."
6667
)
6768
if (
68-
isinstance(expression, models.Aggregate)
69+
isinstance(expression, Aggregate)
6970
and expression.distinct
7071
and len(expression.source_expressions) > 1
7172
):

plain-models/tests/test_delete_behaviors.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
DeleteParent,
99
)
1010

11-
from plain.models import (
12-
ProtectedError,
13-
RestrictedError,
14-
)
11+
from plain.models.deletion import ProtectedError, RestrictedError
1512

1613

1714
def _create_parents():

plain-oauth/plain/oauth/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
AdminViewset,
66
register_viewset,
77
)
8-
from plain.models import Count
8+
from plain.models.aggregates import Count
99

1010
from .models import OAuthConnection
1111

plain-oauth/plain/oauth/preflight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from plain.models import OperationalError, ProgrammingError
1+
from plain.models.db import OperationalError, ProgrammingError
22
from plain.preflight import PreflightCheck, PreflightResult, register_check
33

44

0 commit comments

Comments
 (0)