You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
apps.get_model() wrongfully returns latest model types #3584
For apps.get_model(), this plugin returns types declared as defined in models.py.
How is that should be
apps.get_model will return a runtime-generated model with the fields that the model had in the context where it is being used, but almost never the "latest" version of a model.
For example, suppose a system has 3 migrations:
Create model Test with fields hello and goodbye.
apps.get_model("myapp", "Test").
Remove field goodbye from model Test.
In (2), this plugin will resolve a type which is missing field goodbye, but the actual class returned there does have that field, and that's the whole point of using apps.get_model.
Notes
I suspect that properly implementing this feature is wildly complex, since you basically need to run all the migration machinery in context when resolving.
I'd prefer the previous behaviour (not resolving these types) rather than resolving the wrong ones.
Activity
sobolevn commentedon Aug 11, 2026
How is
appsannotated?from django.db.migrations.state import StateApps?See #3511 (comment)
WhyNotHugo commentedon Aug 11, 2026
No, it annotated as django.apps.registry.Apps.
https://docs.djangoproject.com/en/6.0/ref/migration-operations/#django.db.migrations.operations.RunPython
sobolevn commentedon Aug 11, 2026
StateAppsis a subclass ofApps, butStateAppsis used here: https://github.com/django/django/blob/d4b02a7bf937b865e2ffc1584e36f1e753c689b9/django/db/migrations/operations/special.py#L194-L199 :)Please, fix your annotation.
WhyNotHugo commentedon Aug 11, 2026
I'm annotating using the class in Django's documentation.
Using type annotations for an internal undocumented class is a very nasty hack.
WhyNotHugo commentedon Aug 11, 2026
Please re-open this to at least give visibility to the bug, even if it's not considered urgent.
sobolevn commentedon Aug 11, 2026
Probably this needs to be updated in the Django docs :)
UnknownPlatypus commentedon Aug 11, 2026
Given a few people already got tripped by that should we consider skipping in migration files like I suggested in #3511 (comment) ?
sobolevn commentedon Aug 11, 2026
I also use
StateAppsin migration testing: https://github.com/wemake-services/django-test-migrationsIt would be very stange to have different types based on the code location :/
I don't think that we ever should do this.