Django/Wagtail Rest API URL过滤器没有响应



我正在使用Wagtail,我有一个名为127.0.0.1:8000/api/v2/stories的API。在API中,我有这个JSON响应

{
"count": 81,
"results": [
{
"id": 122,
"title": "Test Blog",
"blog_authors": [
{
"id": 82,
"meta": {
"type": "blog.BlogAuthorsOrderable"
},
"author_name": "Test",
"author_website": null,
"author_status": false,
},
{
"id": 121,
"title": "Test Blog 1",
"blog_authors": [
{
"id": 81,
"meta": {
"type": "blog.BlogAuthorsOrderable"
},
"author_name": "Test",
"author_website": null,
"author_status": false,
},
}

我获取的主要问题是,我想按作者名称进行筛选。我已经在URL?author_name=Test&?blog_authors__author_name=Test&?author__name=Test但反应是

{
"message": "query parameter is not an operation or a recognised field: author_name"
}

我在known_query_parameters中添加了这些字段,但响应与api/v2/stories/相同。我试过DjangFilterBackend,但每次都得到相同的响应。如何通过author_name&API中的author_status

这是我的api.py

class ProdPagesAPIViewSet(BaseAPIViewSet):
renderer_classes = [JSONRenderer]
pagination_class = CustomPagination
filter_backends = [FieldsFilter,
ChildOfFilter,
AncestorOfFilter,
DescendantOfFilter,
OrderingFilter,
TranslationOfFilter,
LocaleFilter,
SearchFilter,]
known_query_parameters = frozenset(
[
"limit",
"offset",
"fields",
"order",
"search",
"search_operator",
# Used by jQuery for cache-busting. See #1671
"_",
# Required by BrowsableAPIRenderer
"format",
"page","author_name",
]
)
meta_fields = ["type","seo_title","search_description","first_published_at"]
body_fields = ["id","type","seo_title","search_description","first_published_at","title"]
listing_default_fields = ["type","seo_title","search_description","first_published_at","id","title","alternative_title","news_slug","blog_image","video_thumbnail","categories","blog_authors","excerpt","content","content2","tags","story_type"]
nested_default_fields = []
def get_queryset(self):
return super().get_queryset().filter(story_type='Story').order_by('-first_published_at')
name = "stories"
model = AddStory
api_router.register_endpoint("stories", ProdPagesAPIViewSet)

对于wagtail视图,可以在这里找到

这是我的博客作者models.py

class BlogAuthorsOrderable(Orderable):
"""This allows us to select one or more blog authors from Snippets."""
page = ParentalKey("blog.AddStory", related_name="blog_authors")
author = models.ForeignKey(
"blog.BlogAuthor",
on_delete=models.CASCADE,
)
panels = [
# Use a SnippetChooserPanel because blog.BlogAuthor is registered as a snippet
FieldPanel("author"),
]
@property
def author_name(self):
return self.author.name
@property
def author_website(self):
return self.author.website
@property
def author_image(self):
return self.author.image

@property
def author_status(self):
return self.author.status
api_fields = [
APIField("author_name"),
APIField("author_website"),
APIField("author_status"),
# This is using a custom django rest framework serializer
APIField("author_image", serializer=ImageSerializedField()),
# The below APIField is using a Wagtail-built DRF Serializer that supports
# custom image rendition sizes
APIField(
"image",
serializer=ImageRenditionField("fill-200x250|format-webp",
source="author_image"
)
),
]

@register_snippet
class BlogAuthor(models.Model):
"""Blog author for snippets."""

name = models.CharField(max_length=100)
status = models.BooleanField(default=False, verbose_name="Special Author")
website = models.URLField(blank=True, null=True)
image = models.ForeignKey(
"wagtailimages.Image",
on_delete=models.SET_NULL,
null=True,
blank=False,
related_name="+",
)
panels = [
MultiFieldPanel(
[
FieldPanel("name"),
# Use an ImageChooserPanel because wagtailimages.Image (image property)
# is a ForeignKey to an Image
FieldPanel("image"),
FieldPanel("status"),
],
heading="Name, Image and Status",
),
MultiFieldPanel(
[
FieldPanel("website"),
],
heading="Links"
)
]
def __str__(self):
"""String repr of this class."""
return self.name
class Meta:  # noqa
verbose_name = "Blog Author"
verbose_name_plural = "Blog Authors"

以下是我如何称呼博客作者

class AddStory(Page):
"""Blog detail page."""
content_panels = Page.content_panels + [
MultiFieldPanel(
[
InlinePanel("blog_authors", label="Author", min_num=1, max_num=4)
],
heading="Author(s)"
),

]
api_fields = [
APIField("blog_authors"),
]

您需要添加一个自定义过滤器来处理author_name参数。就像Wagtail本身提供的过滤器一样,它可能应该继承自from rest_framework.filters import BaseFilterBackend

最新更新