[Django]-Django: typehinting backward / related_name / ForeignKey relationships

6👍

Django adds backwards relations at runtime which aren’t caught by mypy which only does static analysis.

To make mypy happy (and to make it work with your editor’s autocomplete) you need to add an explicit type hint to Site:

class Site(models.Model):
    organization: "Organization"

class Organization(models.Model):
    site = models.OneToOneField(Site)

Using quotes around the type is needed since we are doing a forward reference to Organization before it has been defined.

For foreign keys and many-to-many relationships, you can do the same thing, but using a QuerySet type hint instead:

class Organization(models.Model):
    site = models.OneToOneField(Site)
    employees: models.QuerySet["Employee"]

class Employee(models.Model):
    organization = models.ForeignKey(
        Organization,
        on_delete=models.CASCADE,
        related_name="employees",
    )

EDIT: There is a django-stubs package which is meant to integrate with mypy, however I haven’t used it personally. It may provide a solution for this without having to explicitly add type hints to models.

👤Jessie

-3👍

I just know a litle of english, and i don’t know this is true. But you can add

site = models.OneToOneField(Site, on_delete=models.CASCADE)

Leave a comment