Reputation: 2033
Let's say we have the following models:
class Site(models.Model):
# This is djangos build-in Site Model
pass
class Organization(models.Model):
site = models.OneToOneField(Site)
And if I use this somewhere in some other class:
organization = self.site.organization
Then mypy complains:
Site has no attribute "organization"
How can I make mypy happy here?
Upvotes: 7
Views: 4433
Reputation: 2495
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.
Upvotes: 16
Reputation: 7
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)
Upvotes: -6