Opened 10 years ago
Last modified 10 years ago
#24231 closed Bug
Regression in availability of `_meta.get_field()` before app registry is fully populated — at Version 1
Reported by: | Carl Meyer | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.8alpha1 |
Severity: | Normal | Keywords: | 1.8-beta |
Cc: | pirosb3 | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
In Django versions prior to 1.8, it was possible to call _meta.get_field(...)
on a model class before the app registry was fully populated -- for instance, in a class_prepared
signal handler that adjusts a model's fields or adds new field(s). (Real-world example: https://github.com/carljm/django-model-utils/blob/master/model_utils/models.py#L54-L94)
In 1.8, with the meta refactor, this now raises an AppRegistryError
.
This change makes sense, because get_field
also gets reverse related fields, and those can't be populated until all models are done loading. But it's still a regression in a reasonable use case (even though the meta API was technically private before, it was de facto public).
I think the issue of "when are these methods safe to call?" should be discussed in the _meta refactor porting guide, because it has changed from 1.7, even for methods like get_field()
that otherwise remain similar.
I also think there should be some public supported, documented API that is safe to use before the app registry is populated, that accesses only fields declared on the local model. Perhaps an API equivalent to what you can currently do with _meta._get_fields(reverse=False)
. (I note that this last is already used several places in Django internally where local fields need to be accessed before the app registry is ready.)