python - What is the purpose of apps.py in Django? -
this question has been asked earlier: what purpose of apps.py in django 1.9?
application configuration objects store metadata application. attributes can configured in appconfig subclasses. others set django , read-only.
however, mean metadata application? limited appconfig
metadata:name
, verbose_name
, path
, label
, module
, models_module
?
or make sense extends beyonds predefined metadata, application specific metadata, example in blog
apps have date format configuration, defined follows:
# file: settings.py blog = { 'date_format': 'ddmmyyy', }
at being used follow:
# file: blog/<...>.py django.conf import settings date_format = settings.blog['date_format']
on contrary, move configuration blog/apps.py
blogconfig
?
class blogconfig(appconfig): name = 'blog' verbose_name = 'awesome blog' date_format = 'ddmmyyyy'
so throughout code in application, date_format
being used by:
# file: blog/<...>.py django.apps import apps date_format = apps.get_app_config('blog').date_format
it sounds me settings.py
project settings, not application settings. more sounds put application settings inside apps.py
settings.py
. so, valid assumption/argument/convention me put application configuration inside apps.py
instead of settings.py
?
a project unique per django installation, while app supposed reusable.
if put custom app settings in project's settings.py
supposed modifiable, if (or others) reuse app project.
now, if put these custom settings in app's apps.py
, means won't modifiable on per project basis. in case there no reason put them in apps.py
rather in constants
submodule instance. unless want provide 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
blogconfig
project using app able choose customizabledateformatblogconfig
or i18nblogconfig
well.
however makes poorly customizable apps. in example above, if want let app users use both customizabledateformatblogconfig
, i18nblogconfig
, need 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 need provide set of few different app configs, you'd better put custom app settings in project's settings.py
.
Comments
Post a Comment