Skip to content

Commit 1939640

Browse files
committed
add info about custom base models
1 parent 7407b93 commit 1939640

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,32 @@ class MyUser(models.Model):
6767

6868
work, which should make a error messages a bit better.
6969

70-
Otherwise, custom type will be created in mypy, named `MyUser__MyUserManager`, which will rewrite base manager as `models.Manager[User]` to make methods like `get_queryset()` and others return properly typed `QuerySet`.
70+
Otherwise, custom type will be created in mypy, named `MyUser__MyUserManager`, which will rewrite base manager as `models.Manager[User]` to make methods like `get_queryset()` and others return properly typed `QuerySet`.
71+
72+
## FAQ
73+
74+
### Custom base model
75+
76+
If you want to subclass any other `Model` besides `models.Model` from the third-party library,
77+
and it's not yet type annotated, mypy won't recognize as a `models.Model` subclass,
78+
and you'll going to have all kinds of errors, like
79+
```
80+
error: Need type annotation for 'author'
81+
error: Need type annotation for 'short_description'
82+
error: Need type annotation for 'long_description'
83+
```
84+
(see https://github.com/typeddjango/django-stubs/issues/68 for examples).
85+
86+
You can bypass this limitation by declaring your import as
87+
```
88+
if TYPE_CHECKING:
89+
from third_party.models import BaseModel as _BaseModel
90+
91+
class BaseModel(_BaseModel, models.Model):
92+
pass
93+
else:
94+
from third_party.models import BaseModel as _BaseModel
95+
```
7196

7297
## To get help
7398

0 commit comments

Comments
 (0)