我在Wagtail项目中有一些应用程序,其中一个是“新闻”,其中包含新闻(页面)。我想在管理员中将标题的标签“ title”覆盖为“ headline”。
News._meta.get_field("title").verbose_name = "Headline"
结果,我在所有应用程序和页面中都获得了所有标题的标签“标题”。为什么我会有这种奇怪的效果?
更新:
# news/models.py
class NewsDetails(Page):
template = "news/news_details.html"
news_text = RichTextField(features=['h2', 'h3', 'bold', 'italic', 'link', 'ol', 'ul',])
news_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=False,
on_delete=models.SET_NULL,
)
content_panels = Page.content_panels + [
ImageChooserPanel("news_image"),
FieldPanel("news_text")
]
class Meta:
verbose_name = "News"
parent_page_types = ['news.NewsList']
NewsDetails._meta.get_field("title").verbose_name = "Headline"
答案 0 :(得分:3)
此方法的问题在于,当您运行django-admin.py makemigrations
命令时,Django将为Wagtail核心生成迁移(即使它是通过pip安装的)。
为避免这种情况,最好在NewsDetails模型中使用自定义base_form_class
。这种方法也适用于更改其他属性,例如help_text
:
from wagtail.admin.forms import WagtailAdminPageForm
class NewsDetailsForm(WagtailAdminPageForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Manually edit the default forms title attributes:
title = self.fields['title']
title.verbose_name="Headline"
title.help_text="Some headline..."
class NewsDetails(Page):
template = "news/news_details.html"
news_text = RichTextField(features=['h2', 'h3', 'bold', 'italic', 'link', 'ol', 'ul',])
news_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=False,
on_delete=models.SET_NULL,
)
content_panels = Page.content_panels + [
ImageChooserPanel("news_image"),
FieldPanel("news_text")
]
class Meta:
verbose_name = "News"
parent_page_types = ['news.NewsList']
# Important line below:
base_form_class = NewsDetailsForm
答案 1 :(得分:3)
对Nathan的答案进行了些微更改,从而解决了verbose_name
不起作用的Valery问题。设置title.label
而不是title.verbose_name
:
class NewsDetailsForm(WagtailAdminPageForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Manually edit the default form's title attributes:
title = self.fields['title']
title.label = "Headline"
title.help_text = "Some headline..."
原因是我们现在正在处理django.forms.Field而不是django.db.models.Field。请参见django.db.models.Field.formfield()
中的代码:
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
"""Return a django.forms.Field instance for this field."""
defaults = {
'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text,
}
...
return form_class(**defaults)
答案 2 :(得分:0)
也许可以通过在模型类中添加 init 方法,如下所示:
class NewsDetails(Page):
...
def __init__(self, *args, **kwargs):
super(NewsDetails, self).__init__(*args, **kwargs)
self._meta.get_field('title').verbose_name = 'Headline'
...
我认为必须对模型中定义的所有类都执行此操作,否则, Page 中的子类将具有最后更新的标签。例如,在这里,如果我将 Page 子类化以创建一个新类,则将看到 Headline 作为标题标签,而应该看到默认标签 Title 。