Skip to content

Commit 17a378d

Browse files
committed
Change class Meta to _meta descriptor
1 parent eb502c5 commit 17a378d

File tree

31 files changed

+521
-363
lines changed

31 files changed

+521
-363
lines changed

plain-api/plain/api/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,15 @@ class User(models.Model):
221221
required=False,
222222
)
223223

224-
class Meta:
225-
constraints = [
224+
_meta = models.Options(
225+
constraints=[
226226
models.UniqueConstraint(
227227
fields=["api_key"],
228228
condition=models.Q(api_key__isnull=False),
229229
name="unique_user_api_key",
230230
),
231-
]
231+
],
232+
)
232233
```
233234

234235
Generating API keys is something you will need to do in your own code, wherever it makes sense to do so.

plain-api/plain/api/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ class APIKey(models.Model):
2323

2424
api_version = models.CharField(max_length=255, required=False)
2525

26-
class Meta:
27-
constraints = [
26+
_meta = models.Options(
27+
constraints=[
2828
models.UniqueConstraint(
2929
fields=["uuid"], name="plainapi_apikey_unique_uuid"
3030
),
3131
models.UniqueConstraint(
3232
fields=["token"], name="plainapi_apikey_unique_token"
3333
),
34-
]
34+
],
35+
)
3536

3637
def __str__(self) -> str:
3738
return self.name or str(self.uuid)

plain-cache/plain/cache/models.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ class CachedItem(models.Model):
2727

2828
query = CachedItemQuerySet()
2929

30-
class Meta:
31-
indexes = [
30+
_meta = models.Options(
31+
indexes=[
3232
models.Index(fields=["expires_at"]),
33-
]
34-
constraints = [
33+
],
34+
constraints=[
3535
models.UniqueConstraint(
3636
fields=["key"], name="plaincache_cacheditem_unique_key"
3737
),
38-
]
38+
],
39+
)
3940

4041
def __str__(self) -> str:
4142
return self.key

plain-flags/plain/flags/models.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ class FlagResult(models.Model):
2323
key = models.CharField(max_length=255)
2424
value = models.JSONField()
2525

26-
class Meta:
27-
constraints = [
26+
_meta = models.Options(
27+
constraints=[
2828
models.UniqueConstraint(
2929
fields=["flag", "key"], name="plainflags_flagresult_unique_key"
3030
),
31-
]
31+
],
32+
)
3233

3334
def __str__(self) -> str:
3435
return self.key
@@ -50,12 +51,13 @@ class Flag(models.Model):
5051
# To provide an easier way to see if a flag is still being used
5152
used_at = models.DateTimeField(required=False, allow_null=True)
5253

53-
class Meta:
54-
constraints = [
54+
_meta = models.Options(
55+
constraints=[
5556
models.UniqueConstraint(
5657
fields=["name"], name="plainflags_flag_unique_name"
5758
),
58-
]
59+
],
60+
)
5961

6062
def __str__(self) -> str:
6163
return self.name

plain-models/plain/models/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,16 @@ class User(models.Model):
200200
username = models.CharField(max_length=150)
201201
age = models.IntegerField()
202202

203-
class Meta:
204-
indexes = [
203+
_meta = models.Options(
204+
indexes=[
205205
models.Index(fields=["email"]),
206206
models.Index(fields=["-created_at"], name="user_created_idx"),
207-
]
208-
constraints = [
207+
],
208+
constraints=[
209209
models.UniqueConstraint(fields=["email", "username"], name="unique_user"),
210210
models.CheckConstraint(check=models.Q(age__gte=0), name="age_positive"),
211-
]
211+
],
212+
)
212213
```
213214

214215
## Custom QuerySets

plain-models/plain/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
# Imports that would create circular imports if sorted
6565
from .base import DEFERRED, Model # isort:skip
66+
from .options import Options # isort:skip
6667
from .fields.related import ( # isort:skip
6768
ForeignKey,
6869
ManyToManyField,
@@ -104,6 +105,7 @@
104105
"JSONField",
105106
"Lookup",
106107
"Transform",
108+
"Options",
107109
"Prefetch",
108110
"Q",
109111
"QuerySet",

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from decimal import Decimal
55
from typing import TYPE_CHECKING, Any
66

7-
from plain.models import register_model
7+
from plain.models import Options, register_model
88
from plain.models.backends.base.schema import BaseDatabaseSchemaEditor
99
from plain.models.backends.ddl_references import Statement
1010
from plain.models.backends.utils import strip_quotes
@@ -288,29 +288,27 @@ def is_self_referential(f: Field) -> bool:
288288
# table and solely exists for foreign key reference resolution purposes.
289289
# This wouldn't be required if the schema editor was operating on model
290290
# states instead of rendered models.
291-
meta_contents = {
292-
"package_label": model._meta.package_label,
293-
"db_table": model._meta.db_table,
294-
"indexes": indexes,
295-
"constraints": constraints,
296-
"models_registry": models_registry,
297-
}
298-
meta = type("Meta", (), meta_contents)
299-
body_copy["Meta"] = meta
291+
meta_options = Options(
292+
package_label=model._meta.package_label,
293+
db_table=model._meta.db_table,
294+
indexes=indexes,
295+
constraints=constraints,
296+
models_registry=models_registry,
297+
)
298+
body_copy["_meta"] = meta_options
300299
body_copy["__module__"] = model.__module__
301300
register_model(type(model._meta.object_name, model.__bases__, body_copy)) # type: ignore[arg-type]
302301

303302
# Construct a model with a renamed table name.
304303
body_copy = copy.deepcopy(body)
305-
meta_contents = {
306-
"package_label": model._meta.package_label,
307-
"db_table": f"new__{strip_quotes(model._meta.db_table)}",
308-
"indexes": indexes,
309-
"constraints": constraints,
310-
"models_registry": models_registry,
311-
}
312-
meta = type("Meta", (), meta_contents)
313-
body_copy["Meta"] = meta
304+
meta_options = Options(
305+
package_label=model._meta.package_label,
306+
db_table=f"new__{strip_quotes(model._meta.db_table)}",
307+
indexes=indexes,
308+
constraints=constraints,
309+
models_registry=models_registry,
310+
)
311+
body_copy["_meta"] = meta_options
314312
body_copy["__module__"] = model.__module__
315313
new_model = type(f"New{model._meta.object_name}", model.__bases__, body_copy)
316314
register_model(new_model) # type: ignore[arg-type]

0 commit comments

Comments
 (0)