所有 Page 模型(SEOablePage 及其子类 HomePage/BlogPage/BlogIndexPage/ ProductPage/CaseStudyPage/SolutionPage)此前均未声明 api_fields,导致 前端 fields=body,seo_title_override,... 等请求被 Wagtail API v2 拒绝并 返回 400(Wagtail 默认只暴露 id/meta/title,其余字段必须显式加入 api_fields 才能通过 ?fields= 查询)。这正是生产环境首页显示"尚未连接到 Wagtail CMS"的根因。 - apps/core/models.py: SEOablePage 增加 api_fields(seo_title_override/ seo_description_override);SimpleContentPage 追加 body - apps/home/models.py: HomePage 追加 body - apps/blog/models.py: BlogIndexPage 追加 intro;BlogPage 追加 published_at/intro/body - apps/products/models.py: ProductPage 追加 summary/body - apps/cases/models.py: CaseStudyPage 追加 client_name/industry/ published_at/summary/body - apps/solutions/models.py: SolutionPage 追加 industry/summary/body api_fields 是纯 Wagtail 类属性,不是 Django 模型字段,因此不产生新的 migration(makemigrations --check 确认 No changes detected)。 验证:pytest 67 个用例全部通过;manage.py check 无新增错误(仅已知 treebeard.E001 警告)。
87 lines
2.5 KiB
Python
87 lines
2.5 KiB
Python
from django.db import models
|
|
from modelcluster.contrib.taggit import ClusterTaggableManager
|
|
from modelcluster.fields import ParentalKey
|
|
from taggit.models import TaggedItemBase
|
|
from wagtail.admin.panels import FieldPanel
|
|
from wagtail.api import APIField
|
|
from wagtail.fields import StreamField
|
|
from wagtail.search import index
|
|
|
|
from apps.core.blocks import COMMON_BLOCKS
|
|
from apps.core.models import SEOablePage
|
|
|
|
|
|
class BlogIndexPage(SEOablePage):
|
|
"""博客列表页,本身不直接管理内容,子页面为 BlogPage。"""
|
|
|
|
intro = models.TextField(blank=True, verbose_name="栏目简介")
|
|
|
|
content_panels = SEOablePage.content_panels + [
|
|
FieldPanel("intro"),
|
|
]
|
|
|
|
api_fields = SEOablePage.api_fields + [
|
|
APIField("intro"),
|
|
]
|
|
|
|
subpage_types = ["blog.BlogPage"]
|
|
parent_page_types = ["home.HomePage"]
|
|
|
|
class Meta:
|
|
verbose_name = "博客栏目页"
|
|
|
|
def get_context(self, request, *args, **kwargs):
|
|
context = super().get_context(request, *args, **kwargs)
|
|
context["posts"] = (
|
|
BlogPage.objects.live().descendant_of(self).order_by("-published_at")
|
|
)
|
|
return context
|
|
|
|
|
|
class BlogPageTag(TaggedItemBase):
|
|
content_object = ParentalKey(
|
|
"BlogPage", related_name="tagged_items", on_delete=models.CASCADE
|
|
)
|
|
|
|
|
|
class BlogPage(SEOablePage):
|
|
"""技术博客文章页。"""
|
|
|
|
published_at = models.DateTimeField(verbose_name="发布时间")
|
|
intro = models.CharField(max_length=250, blank=True, verbose_name="摘要")
|
|
cover_image = models.ForeignKey(
|
|
"wagtailimages.Image",
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
related_name="+",
|
|
verbose_name="封面图",
|
|
)
|
|
body = StreamField(COMMON_BLOCKS, use_json_field=True, blank=True)
|
|
tags = ClusterTaggableManager(through=BlogPageTag, blank=True, verbose_name="标签")
|
|
|
|
search_fields = SEOablePage.search_fields + [
|
|
index.SearchField("intro"),
|
|
index.SearchField("body"),
|
|
]
|
|
|
|
content_panels = SEOablePage.content_panels + [
|
|
FieldPanel("published_at"),
|
|
FieldPanel("intro"),
|
|
FieldPanel("cover_image"),
|
|
FieldPanel("body"),
|
|
FieldPanel("tags"),
|
|
]
|
|
|
|
api_fields = SEOablePage.api_fields + [
|
|
APIField("published_at"),
|
|
APIField("intro"),
|
|
APIField("body"),
|
|
]
|
|
|
|
parent_page_types = ["blog.BlogIndexPage"]
|
|
subpage_types = []
|
|
|
|
class Meta:
|
|
verbose_name = "博客文章"
|