django/wagtail - object 属性显示 None 时管理面板另有说明?

django/wagtail - object attribute showing None when admin panel states otherwise?

我无法理解为什么当管理面板显示 post object 的所选类别时,我的 {{ post.categories }} 在模板中显示为 blog.PostPageBlogCategory.None .

这是我的 model.py 设置:

from django.db import models

# Create your models here.
from django.db import models

from modelcluster.fields import ParentalKey
from modelcluster.tags import ClusterTaggableManager

from taggit.models import Tag as TaggitTag
from taggit.models import TaggedItemBase

from wagtail.admin.edit_handlers import (
    FieldPanel,
    FieldRowPanel,
    InlinePanel,
    MultiFieldPanel,
    PageChooserPanel,
    StreamFieldPanel,
)
from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet


class BlogPage(Page):
    description = models.CharField(max_length=255, blank=True,)

    content_panels = Page.content_panels + \
        [FieldPanel("description", classname="full")]


class PostPage(Page):
    header_image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    tags = ClusterTaggableManager(through="blog.PostPageTag", blank=True)

    content_panels = Page.content_panels + [
        ImageChooserPanel("header_image"),
        InlinePanel("categories", label="category"),
        FieldPanel("tags"),
    ]


class PostPageBlogCategory(models.Model):
    page = ParentalKey(
        "blog.PostPage", on_delete=models.CASCADE, related_name="categories"
    )
    blog_category = models.ForeignKey(
        "blog.BlogCategory", on_delete=models.CASCADE, related_name="post_pages"
    )

    panels = [
        SnippetChooserPanel("blog_category"),
    ]

    class Meta:
        unique_together = ("page", "blog_category")


@register_snippet
class BlogCategory(models.Model):

    CATEGORY_CHOICES = (
        ('fighter', 'Fighter'),
        ('model', 'Model'),
        ('event', 'Event'),
        ('organization', 'Organization'),
        ('other', 'Other')
    )

    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True, max_length=80)

    category_type = models.CharField(
        max_length=100, choices=CATEGORY_CHOICES,  blank=True)

    description = models.CharField(max_length=500, blank=True)

    panels = [
        FieldPanel("name"),
        FieldPanel("slug"),
        FieldPanel("category_type"),
        FieldPanel("description"),
    ]

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"


class PostPageTag(TaggedItemBase):
    content_object = ParentalKey("PostPage", related_name="post_tags")


@register_snippet
class Tag(TaggitTag):
    class Meta:
        proxy = True

关于我的 blog_page.html 模板的一点上下文。此模板是博客中创建的所有 post 的列表,而不是个人 post 本身。

{% extends "base.html" %} 

{% load wagtailcore_tags wagtailimages_tags %} 

{% block content %}


<section class="text-gray-600 body-font">
  <div class="container px-5 py-24 mx-auto">
    <div class="flex flex-wrap -m-4">
      {% for post in page.get_children.specific %}

      <div class="p-4 md:w-1/3">
        <div
          class="
            h-full
            border-2 border-gray-200 border-opacity-60
            rounded-lg
            overflow-hidden
          "
        >
          {% if post.header_image %} {% image post.header_image original as header_image %}
          <a href="{% pageurl post %}">
            <img
              src="{{ header_image.url }}"
              class="lg:h-48 md:h-36 w-full object-cover object-center"
            />
          </a>
          {% endif %}
          <div class="p-6">


            <h2
              class="
                tracking-widest
                text-xs
                title-font
                font-medium
                text-gray-400
                mb-1
              "
            >
              {{ post.categories}}

            </h2>
       ## The rest omitted for brevity

现在,我可以从 post object 中提取数据,例如日期、标题、图像,但由于某些原因,我在 [=41] 中设置类别的方式=], 我无法为每个 post.

提供正确的类别

这里是模板和管理员的图像以获取更多上下文:

如您所见,blog.PostPageBlogCategory.None 正在为 {{post.categories}} 展示自己。理想情况下,显示的正确类别应该是字符串 object,而不是 None.

我的模型做错了什么?

这是 Django 的一个怪癖...post.categories 本身并没有为您提供类别列表,而是 manager object 提供了对该关系的各种操作。 (我不确定为什么该管理器对象的字符串表示形式显示为 "blog.PostPageBlogCategory.None",但是...)

post.categories.all 将为您提供 PostPageBlogCategory 对象的实际查询集,但由于您没有在 class 上提供 __str__ 方法,因此可能直接输出也不会显示任何有意义的内容。遍历它应该会给你你正在寻找的东西:

{% for post_category in post.categories.all %}
    {{ post_category.blog_category }}
{% endfor %}