首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django/Wagtail Rest Framework排序/分页

Django/Wagtail Rest Framework排序/分页
EN

Stack Overflow用户
提问于 2022-09-16 08:59:28
回答 1查看 32关注 0票数 0

我用的是摇尾。我已经序列化了我的API。我想通过-first_published_at订购它们,当有人点击我的API URL api/v2/pages时,他们将看到一个有序的api/v2/pages,而无需通过url过滤它。以下是我的api.py代码:

代码语言:javascript
复制
class ProdPagesAPIViewSet(BaseAPIViewSet):
    renderer_classes = [JSONRenderer]
    filter_backends = [FieldsFilter,
        ChildOfFilter,
        AncestorOfFilter,
        DescendantOfFilter,
        OrderingFilter,
        TranslationOfFilter,
        LocaleFilter,
        SearchFilter,]
    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",]
    nested_default_fields = []
    ordered_queryset= [] 
    name = "pages"
    model = AddStory

api_router.register_endpoint("pages", ProdPagesAPIViewSet)

我试过ordered_queryset= [AddStory.objects.order_by('-first_published_at')]

但它不是由最新出版的故事订购的。我应该如何进行查询?

这是我的API响应

代码语言:javascript
复制
{
  "meta": {
    "total_count": 6
  },
  "items": [
    {
      "id": 4,
      "meta": {
        "type": "blog.AddStory",
        "seo_title": "",
        "search_description": "",
        "first_published_at": "2022-08-30T11:05:11.341355Z"
      },
    {
      "id": 6,
      "meta": {
        "type": "blog.AddStory",
        "seo_title": "",
        "search_description": "",
        "first_published_at": "2022-08-30T11:13:47.114889Z"
      },
{
  "id": 7,
  "meta": {
    "type": "blog.AddStory",
    "seo_title": "",
    "search_description": "",
    "first_published_at": "2022-08-31T11:13:47.114889Z"
  },
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-17 10:41:38

get_queryset代替order_queryset #api.py解决了

代码语言:javascript
复制
class ProdPagesAPIViewSet(BaseAPIViewSet):
    renderer_classes = [JSONRenderer]
    filter_backends = [FieldsFilter,
        ChildOfFilter,
        AncestorOfFilter,
        DescendantOfFilter,
        OrderingFilter,
        TranslationOfFilter,
        LocaleFilter,
        SearchFilter,]
    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",]
    nested_default_fields = []
    def get_queryset(self):
        return self.model.objects.all().order_by("-first_published_at")
    name = "pages"
    model = AddStory

api_router.register_endpoint("pages", ProdPagesAPIViewSet)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73742401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档