python - django/wagtail - 当管理面板另有说明时,对象属性显示“无”?

标签 python django django-models wagtail

当管理面板显示为 post 对象选择类别。

这是我的 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 模板的一些上下文。此模板是博客中创建的所有帖子的列表,而不是各个帖子本身。

{% 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 对象中提取数据,例如日期、标题、图像,但由于某种原因,我在 models.py 中设置类别的方式,我'我无法为每个帖子提供正确的类别。

这是模板和管理的图像,以了解更多上下文:

enter image description here enter image description here

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

我的模型做错了什么?

最佳答案

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

post.categories.all 将为您提供 PostPageBlogCategory 对象的实际查询集,但由于您尚未提供 __str__ 方法那个类,直接输出可能也不会显示任何有意义的东西。循环它应该会给你你正在寻找的东西:

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

关于python - django/wagtail - 当管理面板另有说明时,对象属性显示“无”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68307310/

相关文章:

python - 重叠坐标的 folium 弹出文本问题(更新 :HTML source code)

python - 散列 frozenset 与 tuple of sorted

python - 使用 django-celery 进行单元测试?

django - 关于Django中批量保存对象的问题

python - 如何添加已经存在的db字段中的数据

python - SPSS 语法或 Python : For each value of x, 有多少个不同的 y 值?

python - 关于 sys.exit 和 SystemExit 的混淆

django - 使用 django-import-export 实现非规范化关系

python - 如何在 django 中扩展用户配置文件?

Django ORM 查询优化问题