| 1 |
from typing import Any, Iterable, Sequence |
| 2 |
|
| 3 |
from django.apps import AppConfig, apps |
| 4 |
from django.core.checks import CheckMessage, Error, Tags, Warning, register |
| 5 |
from django.db import models |
| 6 |
|
| 7 |
|
| 8 |
@register(Tags.models) |
| 9 |
def check_reserved_field_names( |
| 10 |
app_configs: Sequence[AppConfig] | None, **kwargs: Any |
| 11 |
) -> Iterable[CheckMessage]: |
| 12 |
""" |
| 13 |
System check that ensures models don't use reserved field names. |
| 14 |
""" |
| 15 |
from .models import PolymorphicModel |
| 16 |
|
| 17 |
findings: list[CheckMessage] = [] |
| 18 |
|
| 19 |
for app_config in app_configs or apps.get_app_configs(): |
| 20 |
for model in app_config.get_models(): |
| 21 |
if issubclass(model, PolymorphicModel): |
| 22 |
findings.extend(_check_model_reserved_field_names(model)) |
| 23 |
findings.extend(_check_polymorphic_managers(model)) |
| 24 |
|
| 25 |
return findings |
| 26 |
|
| 27 |
|
| 28 |
def _check_polymorphic_managers(model: type[models.Model]) -> list[CheckMessage]: |
| 29 |
from polymorphic.managers import PolymorphicManager |
| 30 |
from polymorphic.query import PolymorphicQuerySet |
| 31 |
|
| 32 |
findings: list[CheckMessage] = [] |
| 33 |
|
| 34 |
# First manager declared with use_in_migrations=True wins. |
| 35 |
for mgr in model._meta.managers: |
| 36 |
if getattr(mgr, "use_in_migrations", True): |
| 37 |
if isinstance(mgr, PolymorphicManager): |
| 38 |
findings.append( |
| 39 |
Error( |
| 40 |
f"The migration manager '{model._meta.label}.{mgr.name}' is polymorphic.", |
| 41 |
obj=mgr, |
| 42 |
hint="Set use_in_migrations = False on the manager.", |
| 43 |
id="polymorphic.E002", |
| 44 |
) |
| 45 |
) |
| 46 |
break |
| 47 |
|
| 48 |
for manager in ["base", "default"]: |
| 49 |
mgr = getattr(model._meta, f"{manager}_manager") |
| 50 |
if not isinstance(mgr, PolymorphicManager): |
| 51 |
findings.append( |
| 52 |
Warning( |
| 53 |
f"The {manager} manager {model._meta.label}.{mgr.name}' is not polymorphic.", |
| 54 |
obj=mgr, |
| 55 |
id="polymorphic.W001", |
| 56 |
) |
| 57 |
) |
| 58 |
if not isinstance(mgr.get_queryset(), PolymorphicQuerySet): |
| 59 |
findings.append( |
| 60 |
Warning( |
| 61 |
f"The {manager} manager {model._meta.label}.{mgr.name}' is not " |
| 62 |
"using a PolymorphicQuerySet.", |
| 63 |
obj=mgr, |
| 64 |
id="polymorphic.W002", |
| 65 |
) |
| 66 |
) |
| 67 |
|
| 68 |
return findings |
| 69 |
|
| 70 |
|
| 71 |
def _check_model_reserved_field_names(model: type[models.Model]) -> list[CheckMessage]: |
| 72 |
from polymorphic.base import POLYMORPHIC_SPECIAL_Q_KWORDS |
| 73 |
|
| 74 |
errors: list[CheckMessage] = [] |
| 75 |
|
| 76 |
for field in model._meta.get_fields(): |
| 77 |
if field.name in POLYMORPHIC_SPECIAL_Q_KWORDS: |
| 78 |
errors.append( |
| 79 |
Error( |
| 80 |
f"Field '{field.name}' on model '{model.__name__}' is a reserved name.", |
| 81 |
obj=field, |
| 82 |
id="polymorphic.E001", |
| 83 |
) |
| 84 |
) |
| 85 |
|
| 86 |
return errors |
| 87 |
|
| 88 |
|
| 89 |
class PolymorphicConfig(AppConfig): |
| 90 |
name: str = "polymorphic" |
| 91 |
verbose_name: str = "Django Polymorphic" |
| 92 |
|
| 93 |
def ready(self) -> None: |
| 94 |
pass |