Replies: 2 comments
-
As far as I know, there is no way to do this because of the dynamic nature of the Django ORM itself and its numerous magic methods like On approach could be to provide a type safe method defined on the model instead of using the ORM directly: the method user can then query your model in a typesafe way, and the potential type unsafety are constrained within this method and not everywhere in your codebase. But the drawbacks could be that you lose some flexibility or a way to chain QuerySet method like you are used to. class Cheese(modesl.Model):
is_good = models.BooleanField()
@classmethod
def get_good_cheeses(cls) -> list["Cheese"]:
return cls.objects.filter(is_good=True) |
Beta Was this translation helpful? Give feedback.
-
This can maybe be improved in the mypy plugin ? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am using vscode with Pylance and I would like to know if it is normal that the types for
Model.objects.filter
arguments areAny
. So if I do this it does not complain:When I ctrl + click it redirects me to this line that shows that there is not type checking for the filter argument.
django-stubs/django-stubs/db/models/manager.pyi
Line 116 in e566001
Is there a way to have type checking for these arguments please?
Beta Was this translation helpful? Give feedback.
All reactions