Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import AppConf using AppConfig.ready() hook #95

Open
paduszyk opened this issue Feb 1, 2024 · 2 comments
Open

Import AppConf using AppConfig.ready() hook #95

paduszyk opened this issue Feb 1, 2024 · 2 comments

Comments

@paduszyk
Copy link

paduszyk commented Feb 1, 2024

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:

import importlib
from contextlib import suppress

from django import apps


class PollsConfig(apps.AppConfig):
    name = "polls"
   
    def ready(self):
        super().ready()
        
        with suppress(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.

@paduszyk paduszyk changed the title Import AppConfs using AppConfig.ready() hook Import AppConf using AppConfig.ready() hook Feb 1, 2024
@carltongibson
Copy link
Contributor

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... 🤔)

@paduszyk
Copy link
Author

@carltongibson You're right, but I will check that ASAP.

Another idea would be to simply:

from .conf import PollsConf

settings = PollsConf()

in the app's __init__.py. Then, one can easily access "local" app-specific settings:

from django.db import models

from . import settings

class ChildModel(models.Model):
    parent = models.ForeignKey(to=settings.PARENT_MODEL, on_delete=models.CASCADE)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants