This recipe will show you how to define settings for your app that can then be overwritten in your project's settings file. This is especially useful for reusable apps that you can customize by adding a configuration.
Defining overwritable app settings
Getting ready
Follow the steps in the Getting ready in the Creating app configuration recipe to create your Django app.
How to do it...
- Define your app settings using the getattr() pattern in models.py if you just have one or two settings, or in the app_settings.py file if the settings are extensive and you want to organize them better:
# myproject/apps/magazine/app_settings.py
from django.conf import settings
from django.utils.translation import gettext_lazy as _
# Example:
SETTING_1 = getattr(settings, "MAGAZINE_SETTING_1", "default value")
MEANING_OF_LIFE = getattr(settings, "MAGAZINE_MEANING_OF_LIFE", 42)
ARTICLE_THEME_CHOICES = getattr(
settings,
"MAGAZINE_ARTICLE_THEME_CHOICES",
[
('futurism', _("Futurism")),
('nostalgia', _("Nostalgia")),
('sustainability', _("Sustainability")),
('wonder', _("Wonder")),
]
)
- models.py will contain the NewsArticle model, like this:
# myproject/apps/magazine/models.py
from django.db import models
from django.utils.translation import gettext_lazy as _
class NewsArticle(models.Model):
created_at = models.DateTimeField(_("Created at"),
auto_now_add=True)
title = models.CharField(_("Title"), max_length=255)
body = models.TextField(_("Body"))
theme = models.CharField(_("Theme"), max_length=20)
class Meta:
verbose_name = _("News Article")
verbose_name_plural = _("News Articles")
def __str__(self):
return self.title
- Next, in admin.py, we will import and use the settings from app_settings.py, as follows:
# myproject/apps/magazine/admin.py
from django import forms
from django.contrib import admin
from .models import NewsArticle
from .app_settings import ARTICLE_THEME_CHOICES
class NewsArticleModelForm(forms.ModelForm):
theme = forms.ChoiceField(
label=NewsArticle._meta.get_field("theme").verbose_name,
choices=ARTICLE_THEME_CHOICES,
required=not NewsArticle._meta.get_field("theme").blank,
)
class Meta:
fields = "__all__"
@admin.register(NewsArticle)
class NewsArticleAdmin(admin.ModelAdmin):
form = NewsArticleModelForm
- If you want to overwrite the ARTICLE_THEME_CHOICES settings for a given project, you should add MAGAZINE_ARTICLE_THEME_CHOICES in the project settings:
# myproject/settings/_base.py
from django.utils.translation import gettext_lazy as _
# ...
MAGAZINE_ARTICLE_THEME_CHOICES = [
('futurism', _("Futurism")),
('nostalgia', _("Nostalgia")),
('sustainability', _("Sustainability")),
('wonder', _("Wonder")),
('positivity', _("Positivity")),
('solutions', _("Solutions")),
('science', _("Science")),
]
How it works...
The getattr(object, attribute_name[, default_value]) Python function tries to get the attribute_name attribute from object and returns default_value if it is not found. We try to read different settings from the Django project settings module or, if they don't exist there, the default values are used.
Note that we could have defined the choices for the theme field in models.py, but instead we created a custom ModelForm in administration and set the choices there. This was done to avoid the creation of new database migrations whenever the ARTICLE_THEME_CHOICES is changed.
See also
- The Creating app configuration recipe
- Chapter 6, Model Administration