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
In docs, you mention that one should trigger the AppConf imports as soon as the Django starts. To do so, you suggest to import the conf modules from the app's models module.
``AppConf`` classes depend on being imported during startup of the Django
process. Even though there are multiple modules loaded automatically,
only the ``models`` modules (usually the ``models.py`` file of your
app) are guaranteed to be loaded at startup. Therefore it's recommended
to put your ``AppConf`` subclass(es) there, too.
I think that a much cleaner (?) solution (how are the models related to configuration, anyway?!) would be to do the import using the ready() hook of the app configuration class (put in the apps.py module).
For example, given an app polls with the conf submodule containing PollsConf(appconf.AppConf), one could go with:
importimportlibfromcontextlibimportsuppressfromdjangoimportappsclassPollsConfig(apps.AppConfig):
name="polls"defready(self):
super().ready()
withsuppress(ImportError):
importlib.import_module(f"{self.name}.conf")
# or simply# from . import conf# (this will make linters unhappy, though ...)
What do you think? Is it worth to be documented? I have recently used this approach and it works.
The text was updated successfully, but these errors were encountered:
paduszyk
changed the title
Import AppConfs using AppConfig.ready() hook
Import AppConf using AppConfig.ready() hook
Feb 1, 2024
Hi @paduszyk — so yeah, that block in the README predates Django's AppConfig by about 3 years. As long as you're not using settings during the setup phase, you're likely good. (The gotcha might be when a model definition uses a setting... 🤔)
To access PARENT_MODEL setting from django.conf.settings, the app prefix must be included. This works fine for my project, but I need to remember to add this for each app.
In docs, you mention that one should trigger the
AppConf
imports as soon as the Django starts. To do so, you suggest to import theconf
modules from the app'smodels
module.django-appconf/README.rst
Lines 45 to 49 in e728a88
I think that a much cleaner (?) solution (how are the models related to configuration, anyway?!) would be to do the import using the
ready()
hook of the app configuration class (put in theapps.py
module).For example, given an app
polls
with theconf
submodule containingPollsConf(appconf.AppConf)
, one could go with:What do you think? Is it worth to be documented? I have recently used this approach and it works.
The text was updated successfully, but these errors were encountered: