python - 使用额外的可订购和片段保存页面时出现问题
问题描述
我在 wagtail 中保存文章页面时遇到了一些问题,我有 3 个类:ArticlePageModule、ArticlePageModulePlacement 和 ArticlePage
错误说:
File "/opt/miniconda3/envs/nefodev/lib/python3.7/site-packages/django/db/models/base.py", line 1222, in full_clean
raise ValidationError(errors)
django.core.exceptions.ValidationError: {'title': ['This field cannot be blank.']}
我的目的是制作一段动态文章,您可以使用如下代码段添加它们:
- 包含几篇文章的页面(仅限预览版)。
- 通过点击文章发送到您自己的文章页面
- 在 wagtail 管理员中,您可以按发布日期管理文章
我的代码(受此代码支持:this)
from django.db import models
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel, InlinePanel
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.core.models import Page, Orderable
from modelcluster.fields import ParentalKey
from wagtail.snippets.models import register_snippet
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.core import blocks
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
# Create your models here.
@register_snippet
class ArticlePageModule(models.Model):
title = models.CharField(max_length=80)
subtitle = models.CharField(max_length=50)
thumbImage = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
panels = [
FieldPanel('title'),
FieldPanel('subtitle'),
ImageChooserPanel('thumbImage')
]
def __str__(self):
return self.title
class ArticlePageModulePlacement(Orderable, models.Model):
page = ParentalKey('blog.ArticlePage', on_delete=models.CASCADE, related_name='article_module_placements')
article_module = models.ForeignKey(ArticlePageModule, on_delete=models.CASCADE, related_name='+')
slug = models.SlugField()
panels = [
FieldPanel('slug'),
SnippetChooserPanel('article_module'),
]
class ArticlePage(Page, RoutablePageMixin):
content = StreamField([
('heading', blocks.CharBlock()),
('content', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
])
content_panels = [
InlinePanel('article_module_placements', label="Modules"),
StreamFieldPanel('content')
]
@route(r'^module/(?P<slug>[\w\-]+)/$')
def page_with_module(self, request, slug=None):
self.article_module_slug = slug
return self.serve(request)
def get_context(self, request):
context = super().get_context(request)
if hasattr(self, 'article_module_slug'):
context['ArticlePageModule'] = self.article_module_placements.filter(slug = self.article_module).first().article_module
return context
解决方案
您已经遗漏了页面标题字段content_panels
- 正如错误消息所述,这是页面模型上的必填字段。通常,你会从Page.content_panels
:
class ArticlePage(Page, RoutablePageMixin):
# ...
content_panels = Page.content_panels + [
InlinePanel('article_module_placements', label="Modules"),
StreamFieldPanel('content')
]
推荐阅读
- python - 有效地将长度为 n 的列表的 pandas 数据帧转换为 n 个数据帧
- laravel - 从多对多数据透视表上的 withPivot 获取关系数据
- java - 如何仅在 *.AUD 表而不是实体中添加特定列?
- ssh - 如何使用其他端口连接远程 SSH VS Code 扩展
- javascript - 在 Typescript 中键入非确定性深度数组
- python - 如何在 Google Colab 的笔记本中显示图像(如在 Anacondan Jupyter Notebook 中)?
- python - 如何在 Discord.py 重写中赋予某人一个角色
- postgresql - 使用 docker-compose 运行 Sonarqube 和 postgresql 时,不遵守声纳仪表板上的用户名和密码
- android - 与水平 RecyclerView 上的选定项目一起滚动的 TextView
- javascript - 减少没有初始值的空数组