首页 > 解决方案 > 通过两个字段而不是一个字段过滤 Wagtail API 响应

问题描述

我正在使用 Wagtail API(建立在 Django Rest Framework 之上)从我们的站点请求信息。我知道我可以使用字段名称和值通过 APIField 过滤响应: https ://docs.wagtail.io/en/v2.5/advanced_topics/api/v2/usage.html#filtering

但是我想允许像这样的复合过滤器: ?slug=my-url-slug&foo=bar

其结果将仅返回符合这两个条件的记录。

更新 这里有一些代码片段供参考:

class LandingPageBase(Page):
    """
    LandingPageBase represents the base information needed for Eventbrite landing pages
    """
    url_slug = models.SlugField(
        max_length=255,
        allow_unicode=True,
        help_text='The slugified portion of a landing page url. Ex: sell-tickets',
    )
    locale = ParentalKey('home.Locale', on_delete=models.PROTECT, related_name='landing_pages')

    api_fields = [
        APIField('url_slug'),
        APIField('locale_code'),
        APIField('locale', serializer=serializers.StringRelatedField(source='locale_code')),
    ]

    @property
    def locale_code(self):
        return self.locale.code

这是我正在使用的 URL。 http://localhost:32815/api/v2/page/?locale_code=de_de&url_slug=my-cool-landing-page-french&type=landing.RawHtmlLandingPage

我将此查询解释为“给我一条具有 X locale_code、Y url_slug 和 Z 类型的记录”,换句话说,所有 AND 语句。但相反,我得到了 url_slug 的记录,而 locale_code 似乎并没有影响结果。

标签: django-rest-frameworkwagtailwagtail-apiv2

解决方案


推荐阅读