Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#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 Aymeric Augustin, 8 years ago

Yes, the local import achieves the same result.

Would you like to suggest a better wording?

comment:2 by Kevin Christopher Henry, 8 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 regular import statement or by using get_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 Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Kevin Christopher Henry, 8 years ago

comment:5 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In ff445f4:

Fixed #26616 -- Clarified model usage in AppConfig.ready().

comment:6 by Tim Graham <timograham@…>, 8 years ago

In 3db01b2d:

[1.10.x] Fixed #26616 -- Clarified model usage in AppConfig.ready().

Backport of ff445f4c19a0fdf6696c99efefa38b1409b8709f from master

Note: See TracTickets for help on using tickets.
Back to Top