plain.models 0.49.2__py3-none-any.whl → 0.51.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. plain/models/CHANGELOG.md +27 -0
  2. plain/models/README.md +26 -42
  3. plain/models/__init__.py +2 -0
  4. plain/models/aggregates.py +42 -19
  5. plain/models/backends/base/base.py +125 -105
  6. plain/models/backends/base/client.py +11 -3
  7. plain/models/backends/base/creation.py +24 -14
  8. plain/models/backends/base/features.py +10 -4
  9. plain/models/backends/base/introspection.py +37 -20
  10. plain/models/backends/base/operations.py +187 -91
  11. plain/models/backends/base/schema.py +338 -218
  12. plain/models/backends/base/validation.py +13 -4
  13. plain/models/backends/ddl_references.py +85 -43
  14. plain/models/backends/mysql/base.py +29 -26
  15. plain/models/backends/mysql/client.py +7 -2
  16. plain/models/backends/mysql/compiler.py +13 -4
  17. plain/models/backends/mysql/creation.py +5 -2
  18. plain/models/backends/mysql/features.py +24 -22
  19. plain/models/backends/mysql/introspection.py +22 -13
  20. plain/models/backends/mysql/operations.py +107 -40
  21. plain/models/backends/mysql/schema.py +52 -28
  22. plain/models/backends/mysql/validation.py +13 -6
  23. plain/models/backends/postgresql/base.py +41 -34
  24. plain/models/backends/postgresql/client.py +7 -2
  25. plain/models/backends/postgresql/creation.py +10 -5
  26. plain/models/backends/postgresql/introspection.py +15 -8
  27. plain/models/backends/postgresql/operations.py +110 -43
  28. plain/models/backends/postgresql/schema.py +88 -49
  29. plain/models/backends/sqlite3/_functions.py +151 -115
  30. plain/models/backends/sqlite3/base.py +37 -23
  31. plain/models/backends/sqlite3/client.py +7 -1
  32. plain/models/backends/sqlite3/creation.py +9 -5
  33. plain/models/backends/sqlite3/features.py +5 -3
  34. plain/models/backends/sqlite3/introspection.py +32 -16
  35. plain/models/backends/sqlite3/operations.py +126 -43
  36. plain/models/backends/sqlite3/schema.py +127 -92
  37. plain/models/backends/utils.py +52 -29
  38. plain/models/backups/cli.py +8 -6
  39. plain/models/backups/clients.py +16 -7
  40. plain/models/backups/core.py +24 -13
  41. plain/models/base.py +221 -229
  42. plain/models/cli.py +98 -67
  43. plain/models/config.py +1 -1
  44. plain/models/connections.py +23 -7
  45. plain/models/constraints.py +79 -56
  46. plain/models/database_url.py +1 -1
  47. plain/models/db.py +6 -2
  48. plain/models/deletion.py +80 -56
  49. plain/models/entrypoints.py +1 -1
  50. plain/models/enums.py +22 -11
  51. plain/models/exceptions.py +23 -8
  52. plain/models/expressions.py +441 -258
  53. plain/models/fields/__init__.py +272 -217
  54. plain/models/fields/json.py +123 -57
  55. plain/models/fields/mixins.py +12 -8
  56. plain/models/fields/related.py +324 -290
  57. plain/models/fields/related_descriptors.py +33 -24
  58. plain/models/fields/related_lookups.py +24 -12
  59. plain/models/fields/related_managers.py +102 -79
  60. plain/models/fields/reverse_related.py +66 -63
  61. plain/models/forms.py +101 -75
  62. plain/models/functions/comparison.py +71 -18
  63. plain/models/functions/datetime.py +79 -29
  64. plain/models/functions/math.py +43 -10
  65. plain/models/functions/mixins.py +24 -7
  66. plain/models/functions/text.py +104 -25
  67. plain/models/functions/window.py +12 -6
  68. plain/models/indexes.py +57 -32
  69. plain/models/lookups.py +228 -153
  70. plain/models/meta.py +505 -0
  71. plain/models/migrations/autodetector.py +86 -43
  72. plain/models/migrations/exceptions.py +7 -3
  73. plain/models/migrations/executor.py +33 -7
  74. plain/models/migrations/graph.py +79 -50
  75. plain/models/migrations/loader.py +45 -22
  76. plain/models/migrations/migration.py +23 -18
  77. plain/models/migrations/operations/base.py +38 -20
  78. plain/models/migrations/operations/fields.py +95 -48
  79. plain/models/migrations/operations/models.py +246 -142
  80. plain/models/migrations/operations/special.py +82 -25
  81. plain/models/migrations/optimizer.py +7 -2
  82. plain/models/migrations/questioner.py +58 -31
  83. plain/models/migrations/recorder.py +27 -16
  84. plain/models/migrations/serializer.py +50 -39
  85. plain/models/migrations/state.py +232 -156
  86. plain/models/migrations/utils.py +30 -14
  87. plain/models/migrations/writer.py +17 -14
  88. plain/models/options.py +189 -518
  89. plain/models/otel.py +16 -6
  90. plain/models/preflight.py +42 -17
  91. plain/models/query.py +400 -251
  92. plain/models/query_utils.py +109 -69
  93. plain/models/registry.py +40 -21
  94. plain/models/sql/compiler.py +190 -127
  95. plain/models/sql/datastructures.py +38 -25
  96. plain/models/sql/query.py +320 -225
  97. plain/models/sql/subqueries.py +36 -25
  98. plain/models/sql/where.py +54 -29
  99. plain/models/test/pytest.py +15 -11
  100. plain/models/test/utils.py +4 -2
  101. plain/models/transaction.py +20 -7
  102. plain/models/utils.py +17 -6
  103. {plain_models-0.49.2.dist-info → plain_models-0.51.0.dist-info}/METADATA +27 -43
  104. plain_models-0.51.0.dist-info/RECORD +123 -0
  105. plain_models-0.49.2.dist-info/RECORD +0 -122
  106. {plain_models-0.49.2.dist-info → plain_models-0.51.0.dist-info}/WHEEL +0 -0
  107. {plain_models-0.49.2.dist-info → plain_models-0.51.0.dist-info}/entry_points.txt +0 -0
  108. {plain_models-0.49.2.dist-info → plain_models-0.51.0.dist-info}/licenses/LICENSE +0 -0
plain/models/options.py CHANGED
@@ -1,521 +1,207 @@
1
- import bisect
2
- import inspect
3
- from collections import defaultdict
4
- from functools import cached_property
1
+ from __future__ import annotations
5
2
 
6
- from plain.models import models_registry
7
- from plain.models.constraints import UniqueConstraint
3
+ from collections.abc import Sequence
4
+ from typing import TYPE_CHECKING, Any
5
+
6
+ from plain.models.backends.utils import truncate_name
8
7
  from plain.models.db import db_connection
9
- from plain.models.exceptions import FieldDoesNotExist
10
- from plain.models.query import QuerySet
11
- from plain.utils.datastructures import ImmutableList
8
+ from plain.packages import packages_registry
12
9
 
13
- PROXY_PARENTS = object()
10
+ if TYPE_CHECKING:
11
+ from plain.models.base import Model
12
+ from plain.models.constraints import BaseConstraint
13
+ from plain.models.indexes import Index
14
14
 
15
- EMPTY_RELATION_TREE = ()
16
15
 
17
- IMMUTABLE_WARNING = (
18
- "The return type of '%s' should never be mutated. If you want to manipulate this "
19
- "list for your own use, make a copy first."
20
- )
16
+ class Options:
17
+ """
18
+ Model options descriptor and container.
19
+
20
+ Acts as both a descriptor (for lazy initialization and access control)
21
+ and the actual options instance (cached per model class).
22
+ """
23
+
24
+ # Type annotations for attributes set in _create_and_cache
25
+ # These exist on cached instances, not on the descriptor itself
26
+ model: type[Model]
27
+ package_label: str
28
+ db_table: str
29
+ db_table_comment: str
30
+ ordering: Sequence[str]
31
+ indexes: Sequence[Index]
32
+ constraints: Sequence[BaseConstraint]
33
+ required_db_features: Sequence[str]
34
+ required_db_vendor: str | None
35
+ _provided_options: set[str]
36
+
37
+ def __init__(
38
+ self,
39
+ *,
40
+ db_table: str | None = None,
41
+ db_table_comment: str | None = None,
42
+ ordering: Sequence[str] | None = None,
43
+ indexes: Sequence[Index] | None = None,
44
+ constraints: Sequence[BaseConstraint] | None = None,
45
+ required_db_features: Sequence[str] | None = None,
46
+ required_db_vendor: str | None = None,
47
+ package_label: str | None = None,
48
+ ):
49
+ """
50
+ Initialize the descriptor with optional configuration.
51
+
52
+ This is called ONCE when defining the base Model class, or when
53
+ a user explicitly sets model_options = Options(...) on their model.
54
+ The descriptor then creates cached instances per model subclass.
55
+ """
56
+ self._config = {
57
+ "db_table": db_table,
58
+ "db_table_comment": db_table_comment,
59
+ "ordering": ordering,
60
+ "indexes": indexes,
61
+ "constraints": constraints,
62
+ "required_db_features": required_db_features,
63
+ "required_db_vendor": required_db_vendor,
64
+ "package_label": package_label,
65
+ }
66
+ self._cache: dict[type[Model], Options] = {}
67
+
68
+ def __get__(self, instance: Any, owner: type[Model]) -> Options:
69
+ """
70
+ Descriptor protocol - returns cached Options for the model class.
71
+
72
+ This is called when accessing Model.model_options and returns a per-class
73
+ cached instance created by _create_and_cache().
74
+
75
+ Can be accessed from both class and instances:
76
+ - MyModel.model_options (class access)
77
+ - my_instance.model_options (instance access - returns class's options)
78
+ """
79
+ # Allow instance access - just return the class's options
80
+ if instance is not None:
81
+ owner = instance.__class__
82
+
83
+ # Skip for the base Model class - return descriptor
84
+ if owner.__name__ == "Model" and owner.__module__ == "plain.models.base":
85
+ return self # type: ignore
86
+
87
+ # Return cached instance or create new one
88
+ if owner not in self._cache:
89
+ return self._create_and_cache(owner)
90
+
91
+ return self._cache[owner]
92
+
93
+ def _create_and_cache(self, model: type[Model]) -> Options:
94
+ """Create Options and cache it."""
95
+ # Create instance without calling __init__
96
+ instance = Options.__new__(Options)
97
+
98
+ # Track which options were explicitly provided by user
99
+ # Note: package_label is excluded because it's passed separately in migrations
100
+ instance._provided_options = {
101
+ k for k, v in self._config.items() if v is not None and k != "package_label"
102
+ }
103
+
104
+ instance.model = model
105
+
106
+ # Resolve package_label
107
+ package_label = self._config.get("package_label")
108
+ if package_label is None:
109
+ module = model.__module__
110
+ package_config = packages_registry.get_containing_package_config(module)
111
+ if package_config is None:
112
+ raise RuntimeError(
113
+ f"Model class {module}.{model.__name__} doesn't declare an explicit "
114
+ "package_label and isn't in an application in INSTALLED_PACKAGES."
115
+ )
116
+ instance.package_label = package_config.package_label
117
+ else:
118
+ instance.package_label = package_label
21
119
 
22
- DEFAULT_NAMES = (
23
- "db_table",
24
- "db_table_comment",
25
- "queryset_class",
26
- "ordering",
27
- "package_label",
28
- "models_registry",
29
- "required_db_features",
30
- "required_db_vendor",
31
- "indexes",
32
- "constraints",
33
- )
120
+ # Set db_table
121
+ db_table = self._config.get("db_table")
122
+ if db_table is None:
123
+ instance.db_table = truncate_name(
124
+ f"{instance.package_label}_{model.__name__.lower()}",
125
+ db_connection.ops.max_name_length(),
126
+ )
127
+ else:
128
+ instance.db_table = db_table
34
129
 
130
+ instance.db_table_comment = self._config.get("db_table_comment") or ""
131
+ instance.ordering = self._config.get("ordering") or []
132
+ instance.indexes = self._config.get("indexes") or []
133
+ instance.constraints = self._config.get("constraints") or []
134
+ instance.required_db_features = self._config.get("required_db_features") or []
135
+ instance.required_db_vendor = self._config.get("required_db_vendor")
35
136
 
36
- def make_immutable_fields_list(name, data):
37
- return ImmutableList(data, warning=IMMUTABLE_WARNING % name)
137
+ # Format names with class interpolation
138
+ instance.constraints = instance._format_names_with_class(instance.constraints)
139
+ instance.indexes = instance._format_names_with_class(instance.indexes)
38
140
 
141
+ # Cache early to prevent recursion if needed
142
+ self._cache[model] = instance
39
143
 
40
- class Options:
41
- FORWARD_PROPERTIES = {
42
- "fields",
43
- "many_to_many",
44
- "concrete_fields",
45
- "local_concrete_fields",
46
- "_non_pk_concrete_field_names",
47
- "_forward_fields_map",
48
- "base_queryset",
49
- "queryset",
50
- }
51
- REVERSE_PROPERTIES = {"related_objects", "fields_map", "_relation_tree"}
52
-
53
- default_models_registry = models_registry
54
-
55
- def __init__(self, meta, package_label=None):
56
- self._get_fields_cache = {}
57
- self.local_fields = []
58
- self.local_many_to_many = []
59
- self.queryset_class = None
60
- self.model_name = None
61
- self.db_table = ""
62
- self.db_table_comment = ""
63
- self.ordering = []
64
- self.indexes = []
65
- self.constraints = []
66
- self.object_name = None
67
- self.package_label = package_label
68
- self.required_db_features = []
69
- self.required_db_vendor = None
70
- self.meta = meta
71
-
72
- # List of all lookups defined in ForeignKey 'limit_choices_to' options
73
- # from *other* models. Needed for some admin checks. Internal use only.
74
- self.related_fkey_lookups = []
75
-
76
- # A custom app registry to use, if you're making a separate model set.
77
- self.models_registry = self.default_models_registry
144
+ return instance
78
145
 
79
146
  @property
80
- def label(self):
81
- return f"{self.package_label}.{self.object_name}"
147
+ def object_name(self) -> str:
148
+ """The model class name."""
149
+ return self.model.__name__
82
150
 
83
151
  @property
84
- def label_lower(self):
85
- return f"{self.package_label}.{self.model_name}"
152
+ def model_name(self) -> str:
153
+ """The model class name in lowercase."""
154
+ return self.object_name.lower()
86
155
 
87
- def contribute_to_class(self, cls, name):
88
- from plain.models.backends.utils import truncate_name
89
-
90
- cls._meta = self
91
- self.model = cls
92
- # First, construct the default values for these options.
93
- self.object_name = cls.__name__
94
- self.model_name = self.object_name.lower()
95
-
96
- # Store the original user-defined values for each option,
97
- # for use when serializing the model definition
98
- self.original_attrs = {}
99
-
100
- # Next, apply any overridden values from 'class Meta'.
101
- if self.meta:
102
- meta_attrs = self.meta.__dict__.copy()
103
- for name in self.meta.__dict__:
104
- # Ignore any private attributes that Plain doesn't care about.
105
- # NOTE: We can't modify a dictionary's contents while looping
106
- # over it, so we loop over the *original* dictionary instead.
107
- if name.startswith("_"):
108
- del meta_attrs[name]
109
- for attr_name in DEFAULT_NAMES:
110
- if attr_name in meta_attrs:
111
- setattr(self, attr_name, meta_attrs.pop(attr_name))
112
- self.original_attrs[attr_name] = getattr(self, attr_name)
113
- elif hasattr(self.meta, attr_name):
114
- setattr(self, attr_name, getattr(self.meta, attr_name))
115
- self.original_attrs[attr_name] = getattr(self, attr_name)
116
-
117
- # Package label/class name interpolation for names of constraints and
118
- # indexes.
119
- for attr_name in {"constraints", "indexes"}:
120
- objs = getattr(self, attr_name, [])
121
- setattr(self, attr_name, self._format_names_with_class(cls, objs))
122
-
123
- # Any leftover attributes must be invalid.
124
- if meta_attrs != {}:
125
- raise TypeError(
126
- "'class Meta' got invalid attribute(s): {}".format(
127
- ",".join(meta_attrs)
128
- )
129
- )
130
-
131
- del self.meta
156
+ @property
157
+ def label(self) -> str:
158
+ """The model label: package_label.ClassName"""
159
+ return f"{self.package_label}.{self.object_name}"
132
160
 
133
- # If the db_table wasn't provided, use the package_label + model_name.
134
- if not self.db_table:
135
- self.db_table = f"{self.package_label}_{self.model_name}"
136
- self.db_table = truncate_name(
137
- self.db_table,
138
- db_connection.ops.max_name_length(),
139
- )
161
+ @property
162
+ def label_lower(self) -> str:
163
+ """The model label in lowercase: package_label.classname"""
164
+ return f"{self.package_label}.{self.model_name}"
140
165
 
141
- def _format_names_with_class(self, cls, objs):
166
+ def _format_names_with_class(self, objs: list[Any]) -> list[Any]:
142
167
  """Package label/class name interpolation for object names."""
143
168
  new_objs = []
144
169
  for obj in objs:
145
170
  obj = obj.clone()
146
171
  obj.name = obj.name % {
147
- "package_label": cls._meta.package_label.lower(),
148
- "class": cls.__name__.lower(),
172
+ "package_label": self.package_label.lower(),
173
+ "class": self.model.__name__.lower(),
149
174
  }
150
175
  new_objs.append(obj)
151
176
  return new_objs
152
177
 
153
- def add_field(self, field):
154
- # Insert the given field in the order in which it was created, using
155
- # the "creation_counter" attribute of the field.
156
- # Move many-to-many related fields from self.fields into
157
- # self.many_to_many.
158
- if field.is_relation and field.many_to_many:
159
- bisect.insort(self.local_many_to_many, field)
160
- else:
161
- bisect.insort(self.local_fields, field)
162
-
163
- # If the field being added is a relation to another known field,
164
- # expire the cache on this field and the forward cache on the field
165
- # being referenced, because there will be new relationships in the
166
- # cache. Otherwise, expire the cache of references *to* this field.
167
- # The mechanism for getting at the related model is slightly odd -
168
- # ideally, we'd just ask for field.related_model. However, related_model
169
- # is a cached property, and all the models haven't been loaded yet, so
170
- # we need to make sure we don't cache a string reference.
171
- if (
172
- field.is_relation
173
- and hasattr(field.remote_field, "model")
174
- and field.remote_field.model
175
- ):
176
- try:
177
- field.remote_field.model._meta._expire_cache(forward=False)
178
- except AttributeError:
179
- pass
180
- self._expire_cache()
181
- else:
182
- self._expire_cache(reverse=False)
183
-
184
- def __repr__(self):
185
- return f"<Options for {self.object_name}>"
186
-
187
- def __str__(self):
188
- return self.label_lower
189
-
190
- def can_migrate(self, connection):
191
- """
192
- Return True if the model can/should be migrated on the given
193
- `connection` object.
194
- """
195
- if self.required_db_vendor:
196
- return self.required_db_vendor == connection.vendor
197
- if self.required_db_features:
198
- return all(
199
- getattr(connection.features, feat, False)
200
- for feat in self.required_db_features
201
- )
202
- return True
178
+ def export_for_migrations(self) -> dict[str, Any]:
179
+ """Export user-provided options for migrations."""
180
+ options = {}
181
+ for name in self._provided_options:
182
+ if name == "indexes":
183
+ # Clone indexes and ensure names are set
184
+ indexes = [idx.clone() for idx in self.indexes]
185
+ for index in indexes:
186
+ if not index.name:
187
+ index.set_name_with_model(self.model)
188
+ options["indexes"] = indexes
189
+ elif name == "constraints":
190
+ # Clone constraints
191
+ options["constraints"] = [con.clone() for con in self.constraints]
192
+ else:
193
+ # Use current attribute value
194
+ options[name] = getattr(self, name)
195
+ return options
203
196
 
204
197
  @property
205
- def base_queryset(self):
206
- """
207
- The base queryset is used by Plain's internal operations like cascading
208
- deletes, migrations, and related object lookups. It provides access to
209
- all objects in the database without any filtering, ensuring Plain can
210
- always see the complete dataset when performing framework operations.
211
-
212
- Unlike user-defined querysets which may filter results (e.g. only active
213
- objects), the base queryset must never filter out rows to prevent
214
- incomplete results in related queries.
215
- """
216
- return QuerySet(model=self.model)
217
-
218
- @property
219
- def queryset(self):
220
- if self.queryset_class:
221
- return self.queryset_class(model=self.model)
222
- return QuerySet(model=self.model)
223
-
224
- @cached_property
225
- def fields(self):
226
- """
227
- Return a list of all forward fields on the model and its parents,
228
- excluding ManyToManyFields.
229
-
230
- Private API intended only to be used by Plain itself; get_fields()
231
- combined with filtering of field properties is the public API for
232
- obtaining this field list.
233
- """
234
-
235
- # For legacy reasons, the fields property should only contain forward
236
- # fields that are not private or with a m2m cardinality. Therefore we
237
- # pass these three filters as filters to the generator.
238
- # The third lambda is a longwinded way of checking f.related_model - we don't
239
- # use that property directly because related_model is a cached property,
240
- # and all the models may not have been loaded yet; we don't want to cache
241
- # the string reference to the related_model.
242
- def is_not_an_m2m_field(f):
243
- return not (f.is_relation and f.many_to_many)
244
-
245
- def is_not_a_generic_relation(f):
246
- return not (f.is_relation and f.one_to_many)
247
-
248
- def is_not_a_generic_foreign_key(f):
249
- return not (
250
- f.is_relation
251
- and f.many_to_one
252
- and not (hasattr(f.remote_field, "model") and f.remote_field.model)
253
- )
254
-
255
- return make_immutable_fields_list(
256
- "fields",
257
- (
258
- f
259
- for f in self._get_fields(reverse=False)
260
- if is_not_an_m2m_field(f)
261
- and is_not_a_generic_relation(f)
262
- and is_not_a_generic_foreign_key(f)
263
- ),
264
- )
265
-
266
- @cached_property
267
- def concrete_fields(self):
268
- """
269
- Return a list of all concrete fields on the model and its parents.
270
-
271
- Private API intended only to be used by Plain itself; get_fields()
272
- combined with filtering of field properties is the public API for
273
- obtaining this field list.
274
- """
275
- return make_immutable_fields_list(
276
- "concrete_fields", (f for f in self.fields if f.concrete)
277
- )
278
-
279
- @cached_property
280
- def local_concrete_fields(self):
281
- """
282
- Return a list of all concrete fields on the model.
283
-
284
- Private API intended only to be used by Plain itself; get_fields()
285
- combined with filtering of field properties is the public API for
286
- obtaining this field list.
287
- """
288
- return make_immutable_fields_list(
289
- "local_concrete_fields", (f for f in self.local_fields if f.concrete)
290
- )
291
-
292
- @cached_property
293
- def many_to_many(self):
294
- """
295
- Return a list of all many to many fields on the model and its parents.
296
-
297
- Private API intended only to be used by Plain itself; get_fields()
298
- combined with filtering of field properties is the public API for
299
- obtaining this list.
300
- """
301
- return make_immutable_fields_list(
302
- "many_to_many",
303
- (
304
- f
305
- for f in self._get_fields(reverse=False)
306
- if f.is_relation and f.many_to_many
307
- ),
308
- )
309
-
310
- @cached_property
311
- def related_objects(self):
312
- """
313
- Return all related objects pointing to the current model. The related
314
- objects can come from a one-to-one, one-to-many, or many-to-many field
315
- relation type.
316
-
317
- Private API intended only to be used by Plain itself; get_fields()
318
- combined with filtering of field properties is the public API for
319
- obtaining this field list.
320
- """
321
- all_related_fields = self._get_fields(
322
- forward=False, reverse=True, include_hidden=True
323
- )
324
- return make_immutable_fields_list(
325
- "related_objects",
326
- (
327
- obj
328
- for obj in all_related_fields
329
- if not obj.hidden or obj.field.many_to_many
330
- ),
331
- )
332
-
333
- @cached_property
334
- def _forward_fields_map(self):
335
- res = {}
336
- fields = self._get_fields(reverse=False)
337
- for field in fields:
338
- res[field.name] = field
339
- # Due to the way Plain's internals work, get_field() should also
340
- # be able to fetch a field by attname. In the case of a concrete
341
- # field with relation, includes the *_id name too
342
- try:
343
- res[field.attname] = field
344
- except AttributeError:
345
- pass
346
- return res
347
-
348
- @cached_property
349
- def fields_map(self):
350
- res = {}
351
- fields = self._get_fields(forward=False, include_hidden=True)
352
- for field in fields:
353
- res[field.name] = field
354
- # Due to the way Plain's internals work, get_field() should also
355
- # be able to fetch a field by attname. In the case of a concrete
356
- # field with relation, includes the *_id name too
357
- try:
358
- res[field.attname] = field
359
- except AttributeError:
360
- pass
361
- return res
362
-
363
- def get_field(self, field_name):
364
- """
365
- Return a field instance given the name of a forward or reverse field.
366
- """
367
- try:
368
- # In order to avoid premature loading of the relation tree
369
- # (expensive) we prefer checking if the field is a forward field.
370
- return self._forward_fields_map[field_name]
371
- except KeyError:
372
- # If the app registry is not ready, reverse fields are
373
- # unavailable, therefore we throw a FieldDoesNotExist exception.
374
- if not self.models_registry.ready:
375
- raise FieldDoesNotExist(
376
- f"{self.object_name} has no field named '{field_name}'. The app cache isn't ready yet, "
377
- "so if this is an auto-created related field, it won't "
378
- "be available yet."
379
- )
380
-
381
- try:
382
- # Retrieve field instance by name from cached or just-computed
383
- # field map.
384
- return self.fields_map[field_name]
385
- except KeyError:
386
- raise FieldDoesNotExist(
387
- f"{self.object_name} has no field named '{field_name}'"
388
- )
389
-
390
- def _populate_directed_relation_graph(self):
391
- """
392
- This method is used by each model to find its reverse objects. As this
393
- method is very expensive and is accessed frequently (it looks up every
394
- field in a model, in every app), it is computed on first access and then
395
- is set as a property on every model.
396
- """
397
- related_objects_graph = defaultdict(list)
398
-
399
- all_models = self.models_registry.get_models()
400
- for model in all_models:
401
- opts = model._meta
402
-
403
- fields_with_relations = (
404
- f
405
- for f in opts._get_fields(reverse=False)
406
- if f.is_relation and f.related_model is not None
407
- )
408
- for f in fields_with_relations:
409
- if not isinstance(f.remote_field.model, str):
410
- remote_label = f.remote_field.model._meta.label
411
- related_objects_graph[remote_label].append(f)
412
-
413
- for model in all_models:
414
- # Set the relation_tree using the internal __dict__. In this way
415
- # we avoid calling the cached property. In attribute lookup,
416
- # __dict__ takes precedence over a data descriptor (such as
417
- # @cached_property). This means that the _meta._relation_tree is
418
- # only called if related_objects is not in __dict__.
419
- related_objects = related_objects_graph[model._meta.label]
420
- model._meta.__dict__["_relation_tree"] = related_objects
421
- # It seems it is possible that self is not in all_models, so guard
422
- # against that with default for get().
423
- return self.__dict__.get("_relation_tree", EMPTY_RELATION_TREE)
424
-
425
- @cached_property
426
- def _relation_tree(self):
427
- return self._populate_directed_relation_graph()
428
-
429
- def _expire_cache(self, forward=True, reverse=True):
430
- # This method is usually called by packages.cache_clear(), when the
431
- # registry is finalized, or when a new field is added.
432
- if forward:
433
- for cache_key in self.FORWARD_PROPERTIES:
434
- if cache_key in self.__dict__:
435
- delattr(self, cache_key)
436
- if reverse:
437
- for cache_key in self.REVERSE_PROPERTIES:
438
- if cache_key in self.__dict__:
439
- delattr(self, cache_key)
440
- self._get_fields_cache = {}
441
-
442
- def get_fields(self, include_hidden=False):
443
- """
444
- Return a list of fields associated to the model. By default, include
445
- forward and reverse fields, fields derived from inheritance, but not
446
- hidden fields. The returned fields can be changed using the parameters:
447
-
448
- - include_hidden: include fields that have a related_name that
449
- starts with a "+"
450
- """
451
- return self._get_fields(include_hidden=include_hidden)
452
-
453
- def _get_fields(
454
- self,
455
- forward=True,
456
- reverse=True,
457
- include_hidden=False,
458
- seen_models=None,
459
- ):
460
- """
461
- Internal helper function to return fields of the model.
462
- * If forward=True, then fields defined on this model are returned.
463
- * If reverse=True, then relations pointing to this model are returned.
464
- * If include_hidden=True, then fields with is_hidden=True are returned.
465
- """
466
-
467
- # This helper function is used to allow recursion in ``get_fields()``
468
- # implementation and to provide a fast way for Plain's internals to
469
- # access specific subsets of fields.
470
-
471
- # We must keep track of which models we have already seen. Otherwise we
472
- # could include the same field multiple times from different models.
473
- topmost_call = seen_models is None
474
- if topmost_call:
475
- seen_models = set()
476
- seen_models.add(self.model)
477
-
478
- # Creates a cache key composed of all arguments
479
- cache_key = (forward, reverse, include_hidden, topmost_call)
480
-
481
- try:
482
- # In order to avoid list manipulation. Always return a shallow copy
483
- # of the results.
484
- return self._get_fields_cache[cache_key]
485
- except KeyError:
486
- pass
487
-
488
- fields = []
489
-
490
- if reverse:
491
- # Tree is computed once and cached until the app cache is expired.
492
- # It is composed of a list of fields pointing to the current model
493
- # from other models.
494
- all_fields = self._relation_tree
495
- for field in all_fields:
496
- # If hidden fields should be included or the relation is not
497
- # intentionally hidden, add to the fields dict.
498
- if include_hidden or not field.remote_field.hidden:
499
- fields.append(field.remote_field)
500
-
501
- if forward:
502
- fields += self.local_fields
503
- fields += self.local_many_to_many
504
-
505
- # In order to avoid list manipulation. Always
506
- # return a shallow copy of the results
507
- fields = make_immutable_fields_list("get_fields()", fields)
508
-
509
- # Store result into cache for later access
510
- self._get_fields_cache[cache_key] = fields
511
- return fields
512
-
513
- @cached_property
514
- def total_unique_constraints(self):
198
+ def total_unique_constraints(self) -> list[Any]:
515
199
  """
516
200
  Return a list of total unique constraints. Useful for determining set
517
201
  of fields guaranteed to be unique for all rows.
518
202
  """
203
+ from plain.models.constraints import UniqueConstraint
204
+
519
205
  return [
520
206
  constraint
521
207
  for constraint in self.constraints
@@ -526,37 +212,22 @@ class Options:
526
212
  )
527
213
  ]
528
214
 
529
- @cached_property
530
- def _property_names(self):
531
- """Return a set of the names of the properties defined on the model."""
532
- names = []
533
- for name in dir(self.model):
534
- attr = inspect.getattr_static(self.model, name)
535
- if isinstance(attr, property):
536
- names.append(name)
537
- return frozenset(names)
538
-
539
- @cached_property
540
- def _non_pk_concrete_field_names(self):
541
- """
542
- Return a set of the non-primary key concrete field names defined on the model.
215
+ def can_migrate(self, connection: Any) -> bool:
543
216
  """
544
- names = []
545
- for field in self.concrete_fields:
546
- if not field.primary_key:
547
- names.append(field.name)
548
- if field.name != field.attname:
549
- names.append(field.attname)
550
- return frozenset(names)
551
-
552
- @cached_property
553
- def db_returning_fields(self):
554
- """
555
- Private API intended only to be used by Plain itself.
556
- Fields to be returned after a database insert.
217
+ Return True if the model can/should be migrated on the given
218
+ `connection` object.
557
219
  """
558
- return [
559
- field
560
- for field in self._get_fields(forward=True, reverse=False)
561
- if getattr(field, "db_returning", False)
562
- ]
220
+ if self.required_db_vendor:
221
+ return self.required_db_vendor == connection.vendor
222
+ if self.required_db_features:
223
+ return all(
224
+ getattr(connection.features, feat, False)
225
+ for feat in self.required_db_features
226
+ )
227
+ return True
228
+
229
+ def __repr__(self) -> str:
230
+ return f"<Options for {self.model.__name__}>"
231
+
232
+ def __str__(self) -> str:
233
+ return f"{self.package_label}.{self.model.__name__.lower()}"