Django 3 Web Development Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. 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")),
]
)
  1. 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
  1. 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
  1. 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")),
]