Rules
ForwardRelationTypingCodemod
¶
A codemod that will add type annotations to forward relations.
Rule identifier: DJA001
.
Outdated
This codemod is outdated and does not play well with django-stubs
.
Instead, it is recommended to use the corresponding dynamic stub rule
(DJAS001
).
from typing import TYPE_CHECKING
from django.db import models
# Model is imported in an `if TYPE_CHECKING` block if `--type-checking-block` is used.
if TYPE_CHECKING:
# Related model is imported from the corresponding apps models module:
from myproject.reporters.models import Reporter
class Article(models.Model):
# If the field supports `__class_getitem__` at runtime, it is parametrized directly:
reporter = models.ForeignKey["Reporter"](
"reporters.Reporter",
on_delete=models.CASCADE,
)
# Otherwise, an explicit annotation is used. No unnecessary import if model is in the same file.
article_version: "models.OneToOneField[ArticleVersion]" = models.OneToOneField(
"ArticleVersion",
on_delete=models.CASCADE,
)