wagtail - 如何修复由 wagtail 中的 routeablepage 产生的 FieldError
问题描述
我有以下models.py
设置,
class PostList(RoutablePageMixin, Page):
template = "Post_List.html"
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel("intro")
]
subpage_types = [
"PostDetail",
]
parent_page_type = [
"HomePage",
]
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['posts'] = self.posts
context['post_list'] = self
return context
def get_posts(self):
return PostList.objects.descendant_of(self).live()
@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
self.search_type = 'tag'
self.search_term = tag
self.posts = self.get_posts().filter(tag__slug=tag)
return Page.serve(self, request, *args, **kwargs)
@route(r'^category/(?P<category>[-\w]+)/$')
def post_by_category(self, request, category, *args, **kwargs):
self.search_type = 'category'
self.search_term = category
self.posts = self.get_posts().filter(category__slug=category)
return Page.serve(self, request, *args, **kwargs)
@route(r'^$')
def post_list(self, request, *args, **kwargs):
self.posts = self.get_posts()
return Page.serve(self, request, *args, **kwargs)
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", classname='full'),
InlinePanel("rncategory", label="label_category"), # 'label' determines Title & Button name in Wagtail Admin Portal
FieldPanel("tags"),
]
parent_page_type = [
"PostList",
]
subpage_types = [] # Disable "Add CHILD PAGE"
@property
def post_list(self):
return self.get_parent().specific
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['post_list'] = self.post_list
context['post_detail'] = self
return context
@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="rncategory"
)
category = models.ForeignKey(
"Category", on_delete = models.CASCADE, related_name="rnpostdetail"
)
panels = [
SnippetChooserPanel("category"),
]
class Meta:
unique_together = ("page", "category")
class Link_PostDetail_Tag(TaggedItemBase):
content_object = ParentalKey("PostDetail", related_name="rntags", on_delete=models.CASCADE)
尽管我已经有一个中间模型与routablepageurl
?/category/events/
Link_PostDetail_Category
PostDetail
Category
FieldError at /info/post-list/category/events/
Cannot resolve keyword 'category' into field. Choices are: alias_of, alias_of_id, aliases, comments, content_type, content_type_id, depth, draft_title, expire_at, expired, first_published_at, formsubmission, go_live_at, group_permissions, has_unpublished_changes, id, intro, last_published_at, latest_revision_created_at, live, live_revision, live_revision_id, locale, locale_id, locked, locked_at, locked_by, locked_by_id, numchild, owner, owner_id, page_ptr, page_ptr_id, path, redirect, revisions, search_description, seo_title, show_in_menus, sites_rooted_here, slug, subscribers, title, translation_key, url_path, view_restrictions, workflow_states, workflowpage
根据以下 PDB 输出,
[27] > /usr/src/app/IoTSite/models.py(78)post_by_category()
-> self.search_term = category
(Pdb++) pp locals()
{'args': (), 'category': 'events', 'kwargs': {}, 'request': <WSGIRequest: GET '/info/post-list/category/events/'>, 'self': <PostList: Post List>}
(Pdb++) pp self.get_children()[1].specific
<PostDetail: Detail 2>
(Pdb++) pp self.get_children()[1].specific.rncategory.all()
<QuerySet [<Link_PostDetail_Category: Link_PostDetail_Category object (2)>]>
(Pdb++) pp self.get_children()[1].specific.rncategory.all()[0]
<Link_PostDetail_Category: Link_PostDetail_Category object (2)>
(Pdb++) pp self.get_children()[1].specific.rncategory.all()[0].category
<Category: events>
(Pdb++) pp self.get_children()[1].specific.rncategory.all()[0].category.name
'events'
(Pdb++)
问:我应该如何修改我的源代码来修复错误?
解决方案
方法:
def get_posts(self):
return PostList.objects.descendant_of(self).live()
为您提供作为当前页面后代的 PostList 对象的列表。这可能是一个错误——你想要一个 PostDetail 对象的列表,因为这category
是定义关系的地方。
def get_posts(self):
return PostDetail.objects.descendant_of(self).live()
推荐阅读
- c# - 如何解决“调用目标引发异常”。这个错误
- python - 在 python 中绘制 3D 分布
- reactjs - 如何删除 gatsby 项目中的尾部斜杠?
- mikrotik - 使用 Mikrotik 路由器进行 DaloRadius PPPOE 服务器身份验证
- java - 如何处理spring jdbctemplate中的ORA-00942错误
- python - SciKit Learn R-squared 与 Pearson's Correlation R 的平方非常不同
- swiftui - SwiftUI:在“进入全屏”之后将菜单条目附加到“查看”组
- postgresql - 在 Rust 中将时间戳摄取到 postgres 时,如何避免字符串转换?
- python - cvxpy 导入错误:无法读取文件数据
- angular - ng-select中使用addTag时如何调用函数