【发布时间】:2021-09-15 13:14:29
【问题描述】:
如何使标签和类别分开...我有一个用于 SCRC 的标签和一个用于库的标签,但是当我创建博客并选择每个不同的标签时,如何才能做到这一点? 这是我的models.py 文件。谢谢你的任何建议。 这是我的第一个 django wagtail 应用程序,我正在尝试创建一个测试博客。
from django.db import models
from django.utils.translation import deactivate_all
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 BlogsPage(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [FieldPanel("description", classname="full")]
@register_snippet
class BlogsCategory(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=80)
panels = [
FieldPanel("name"),
FieldPanel("slug"),
]
def __str__(self):
return self.name
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
#How am i going to filter this categories ()
@register_snippet
class Tag(TaggitTag):
class Meta:
proxy = True
class PostPageBlogsCategory(models.Model):
page = ParentalKey(
"blogs.PostPage", on_delete=models.CASCADE, related_name="categories"
)
blogs_category = models.ForeignKey(
"blogs.BlogsCategory", on_delete=models.CASCADE, related_name="post_pages"
)
panels = [
SnippetChooserPanel("blogs_category"),
]
class Meta:
unique_together = ("page", "blogs_category")
class PostPageTag(TaggedItemBase):
content_object = ParentalKey("PostPage", related_name="post_tags")
class PostPage(Page):
header_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
tags = ClusterTaggableManager(through="blogs.PostPageTag", blank=True)
content_panels = Page.content_panels + [
ImageChooserPanel("header_image"),
InlinePanel("categories", label="category"),
FieldPanel("tags"),
]
【问题讨论】:
-
您是否考虑过使用 sn-ps 代替?我用它们来标记我建立的网站上的不同类别。如果您想向 sn-p 添加更多细节,它工作得非常好并且具有更大的灵活性。 docs.wagtail.io/en/stable/topics/snippets.html#id1
-
我如何在这里使用 sn-p,我有我的 models.py 和模板页面。
-
对于您的答案,如何在前端查看特定类别中的帖子,例如 I SCRC 和库,当我选择 SCRC 时,我会看到 SCRC 和库的所有帖子,所以有我认为是我在models.py中需要的if语句。我在最初的问题帖子 imade 中添加了我的模型文件的外观。