#26616 closed Cleanup/optimization (fixed)
Documentation: Improve description of AppConfig.ready()
Reported by: | Kevin Christopher Henry | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I find this sentence and example in the documentation a bit confusing:
You cannot import models in modules that define application configuration classes, but you can use
get_model()
to access a model class by name, like this:
def ready(self): MyModel = self.get_model('MyModel')
It seems to be comparing importing at the module level with using get_model()
inside ready(). But if I understand the How applications are loaded section correctly, by the time ready()
is run the models have already been imported, and therefore it would also be fine to do a direct import inside of ready()
:
def ready(self): from .models import MyModel
If that's the case then I think this sentence is confusing. If that's not the case then I've misunderstood How applications are loaded and perhaps it needs to be clarified.
Change History (6)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
Since the point about not importing models at import time is made forcefully and in detail in How applications are loaded, I think this would work:
Subclasses can override this method to perform initialization tasks such as registering signals. It is called as soon as the registry is fully populated.
This is the first point in the application loading process where it's safe for you to import models (see How applications are loaded[link] for more details). You can do so with a regularimport
statement or by usingget_model()
to access a model class by name, like this:
Or something like that. To me the main thing is to make clear that the distinction is between importing models at import time versus in ready()
, not between get_model()
and import
.
comment:3 by , 8 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Yes, the local import achieves the same result.
Would you like to suggest a better wording?