[Fixed]-Can we use apps.py for application-level configuration as a contrast to settings.py for project-level configurations?

6👍

A project is unique per django installation, while an app is supposed to be reusable.

If you put custom app settings in your project’s settings.py they are supposed to be modifiable, especially if you (or others) reuse this app for another project.

Now, if you put these custom settings in your app’s apps.py, this means they won’t be modifiable on a per project basis. In which case there is no reason to put them in apps.py rather than in a constants submodule for instance. Unless you want to provide a limited set of possible configs:

class BlogConfig(AppConfig):
    name = 'blog'
    verbose_name = "Blog"
    date_format = 'ddMMYYYY'


class CustomizableDateFormatBlogConfig(BlogConfig):
    date_format = getattr(settings, 'BLOG_DATE_FORMAT', BlogConfig.date_format)


class I18nBlogConfig(BlogConfig)
    verbose_name = _("Blog")

The default_app_config would be BlogConfig but the project using the app would be able to choose CustomizableDateFormatBlogConfig or I18nBlogConfig as well.

However this makes very poorly customizable apps. In the example above, if you want to let the app users use both CustomizableDateFormatBlogConfig and I18nBlogConfig, you would need to do something like this:

class BlogConfig(AppConfig):
    name = 'blog'
    verbose_name = "Blog"
    date_format = 'ddMMYYYY'


class CustomizableDateFormatMixin:
    date_format = getattr(settings, 'BLOG_DATE_FORMAT', BlogConfig.date_format)


class I18nMixin:
    verbose_name = _("Blog")


class CustomizableDateFormatBlogConfig(CustomizableDateFormatMixin, BlogConfig):
    pass


class I18nBlogConfig(I18nMixin, BlogConfig):
    pass


class I18nCustomizableDateFormatBlogConfig(I18nMixin, CustomizableDateFormatMixin, BlogConfig):
    pass

So, apart specific cases where you need to provide a set of few different app configs, you’d better put your custom app settings in the project’s settings.py.

Leave a comment