Django Stubs

6.0.2 · active · verified Thu Apr 09

Django Stubs provides Mypy type stubs for the Django framework, enabling static type checking for Django projects. It includes a Mypy plugin that understands Django's ORM, models, and settings to provide accurate type inference. The library is actively maintained with frequent updates, often aligning its major versions with Django's release cycle, ensuring compatibility and comprehensive coverage for new Django features.

Warnings

Install

Quickstart

To use django-stubs, you need to configure Mypy to use its plugin. Create or update your `mypy.ini` or `pyproject.toml` file as shown. Replace `my_project.settings` with the actual path to your Django settings module. This configuration allows Mypy to understand Django's dynamic nature, especially with models and querysets.

[mypy]
plugins = mypy_django_plugin.main

[mypy.plugins.django]
django_settings_module = "my_project.settings"

# Example of a Django model file (e.g., my_app/models.py)
# from django.db import models
#
# class MyModel(models.Model):
#     name = models.CharField(max_length=100)
#     value = models.IntegerField(default=0)
#
# # Example of using type hints
# def get_model_by_name(name: str) -> MyModel | None:
#     return MyModel.objects.filter(name=name).first()
#
# # Run mypy from your project root:
# # mypy .

view raw JSON →