Wagtail 字段未显示在页面列表响应中

问题描述 投票:0回答:1

我正在将 Wagtail 与 django 结合使用,并且正在按照教程进行操作。我有一个像这样的博客页面:

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)
    # Add this:
    authors = ParentalManyToManyField("blog.Author", blank=True)

    tags = ClusterTaggableManager(through=BlogPageTag, blank=True)

    api_fields: list[APIField] = [
        APIField("tags"),
        APIField("date"),
        APIField("authors", serializer=AuthorSerializer(many=True)),
        APIField("intro"),
        APIField("body"),
    ]

当我进入详细视图时,我可以看到这些字段,例如http://127.0.0.1:8000/api/v2/pages/5/

{
    "id": 5,
    "meta": {
        "type": "blog.BlogPage",
        "detail_url": "http://localhost/api/v2/pages/5/",
        "html_url": "http://localhost/blog/first-blog-post/",
        "slug": "first-blog-post",
        "show_in_menus": false,
        "seo_title": "",
        "search_description": "",
        "first_published_at": "2023-08-20T04:37:17.102729Z",
        "alias_of": null,
        "parent": {
            "id": 4,
            "meta": {
                "type": "blog.BlogIndexPage",
                "detail_url": "http://localhost/api/v2/pages/4/",
                "html_url": "http://localhost/blog/"
            },
            "title": "Our blog"
        }
    },
    "title": "First blog post",
    "tags": [
        "react"
    ],
    "date": "2023-08-20",
    "authors": [
        {
            "name": "Brad",
            "author_image": "/media/original_images/71UHg51kgKL._AC_SY679_.jpg"
        }
    ],
    "intro": "This is a blog post intro",
    "body": "<p data-block-key=\"q28xv\">Hello World</p>"
}

但是,在列表视图中,同一页面不包含这些字段。我尝试在网址中使用 fields=* 参数,但显示的只是标题

例如http://127.0.0.1:8000/api/v2/pages/

{
    "meta": {
        "total_count": 3
    },
    "items": [
        {
            "id": 3,
            "meta": {
                "type": "home.HomePage",
                "detail_url": "http://localhost/api/v2/pages/3/",
                "html_url": "http://localhost/",
                "slug": "home",
                "first_published_at": "2023-08-20T04:23:02.114917Z"
            },
            "title": "Home"
        },
        {
            "id": 4,
            "meta": {
                "type": "blog.BlogIndexPage",
                "detail_url": "http://localhost/api/v2/pages/4/",
                "html_url": "http://localhost/blog/",
                "slug": "blog",
                "first_published_at": "2023-08-20T04:35:09.759993Z"
            },
            "title": "Our blog"
        },
        {
            "id": 5,
            "meta": {
                "type": "blog.BlogPage",
                "detail_url": "http://localhost/api/v2/pages/5/",
                "html_url": "http://localhost/blog/first-blog-post/",
                "slug": "first-blog-post",
                "first_published_at": "2023-08-20T04:37:17.102729Z"
            },
            "title": "First blog post"
        }
    ]
}

发生了什么事以及如何解决?

django django-rest-framework wagtail wagtail-apiv2
1个回答
0
投票

您如何创建页面列表?看来您可能省略了 ORM 调用的 Specific() 命令。如果没有它,您将获得用于构建查询的类中的对象集合。如果您查询了 Page.obects,您将获得 Page 对象的列表:

In [3]: Page.objects.public().live()
Out[3]: <PageQuerySet [<Page: Root>, <Page: Home Page>, <Page: Blog Index>, 
    <Page: Also a Blog>, <Page: Some Blog>, <Page: Another Blog>, 
    <Page: test readonly>, <Page: Test CSV>]>

如果添加 Specific(),您将获得每个的顶级类:

In [4]: Page.objects.public().live().specific()
Out[4]: <PageQuerySet [<Page: Root>, <HomePage: Home Page>, <BlogPage: Blog Index>, 
    <BlogPage: Also a Blog>, <BlogPage: Some Blog>, <BlogPage: Another Blog>, 
    <BlogPage: test readonly>, <HomePage: Test CSV>]>

您现在可以访问这些项目的所有自定义属性。

© www.soinside.com 2019 - 2024. All rights reserved.