我正在按照本教程将博客添加到 wagtail。以下是我的代码;
class PostDetail(Page):
template = "Post_Detail.html"
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through="Link_PostDetail_Tag", blank=True)
search_fields = Page.search_fields + [
index.SearchField("body"),
]
content_panels = Page.content_panels + [
FieldPanel("body"),
InlinePanel("rn_category", label="label_category"),
FieldPanel("tags"),
]
parent_page_type = [
"PostList",
]
subpage_types = [] # Disable "Add CHILD PAGE"
@register_snippet
class Category(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=100)
panels = [
FieldPanel("name"),
FieldPanel("slug"),
]
def __str__(self):
return self.name
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
@register_snippet
class Tag(TaggitTag):
class Meta:
proxy = True
class Link_PostDetail_Category(models.Model):
page = ParentalKey(
"PostDetail", on_delete = models.CASCADE, related_name="rn_category"
)
category = models.ForeignKey(
"Category", on_delete = models.CASCADE, related_name="rn_post_detail"
)
panels = [
SnippetChooserPanel("category"),
]
class Meta:
unique_together = ("page", "category")
class Link_PostDetail_Tag(TaggedItemBase):
content_object = ParentalKey("PostDetail", related_name="rn_tags", on_delete=models.CASCADE)
数据库迁移后,我可以看到 PostDetail 和 Category 之间的关系表已经创建。
但是,在我通过 PDB 调试期间(PostDetail
类是类的直接子PostList
类),我无法链接到Category
from PostDetail
。哪里不对了 ?
(Pdb++) pp obj.get_children
<bound method MP_Node.get_children of <PostList: Post List>>
(Pdb++) whatis obj.get_children()
<class 'wagtail.core.query.PageQuerySet'>
(Pdb++) whatis obj.get_children()[0]
<class 'wagtail.core.models.Page'>
(Pdb++) pp obj.get_children()[0].__dict__ # I did not see 'rn_category' attribute in the output.
(Pdb++) whatis obj.get_children()[0].rn_category.all
*** AttributeError: 'Page' object has no attribute 'rn_category'