gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:24:07 27 4
gpt4 key购买 nike

我无法理解为什么我的 {{ post.categories }}在模板中展示了 blog.PostPageBlogCategory.None当管理面板显示为 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}} 展示自己.理想情况下,显示的正确 Category 应该是一个字符串对象,而不是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com