Consider the following code fragment related to Django models:
class Machine(HasStatus): # type: ignore
machines: "models.Manager[Machine]" = models.Manager()
number = models.IntegerField(verbose_name="Číslo stroje", unique=True)
#Type of "number" is "IntegerField[Unknown, Unknown]"
class Meta: #"Meta" overrides symbol of same name in class "HasStatus"
verbose_name = "Machine"
I was able to find out how to annotate Manager, but I don't know how to satisfy PyLance for model fields and Meta class. For the model fields, it seems I need to provide two types, but I have no idea which is the second.
Please, provide examples of how to use type annotation for more Django model fields like ForeignKey and others.
django-stubsships withmypyplugin that does a lot of heavy work, including field types resolution. It will not work with other type checkers, unfortunately, because plugin API is not consistent (or even does not exist) in other checkers. Formypy, see readme section. For other checkers, you can trydjango-types- I have never used it, but the main goal of this project is exactly removingmypydependency,Field[SetType, GetType]. I'd suggest you to read doctring [here[(github.com/typeddjango/django-stubs/blob/master/django-stubs/db/…) about field typing in django-stubs. With plugin you do not need to annotate field on assignment at all, this type is inferred.