【问题标题】:Wagtail Admin Forms ordering鹡鸰管理表格订购
【发布时间】:2021-05-16 07:37:35
【问题描述】:

我的管理面板表单中的页面列表类似于屏幕截图。
如何为此或某种排序添加时间排序?我希望具有新表单提交的页面将出现在此列表的顶部。

【问题讨论】:

    标签: wagtail wagtail-admin


    【解决方案1】:

    您可以为您的模型构建一个 QuerySet 并根据您的需要设置排序。引用documentation

    您必须在构造 QuerySet 时显式应用排序:

    news_items = NewsItemPage.objects.live().order_by('-publication_date')
    

    假设您有一个列出 Form 模型的 FormIndex 页面,您可以像这样更新其上下文:

    class FormIndexPage(Page):
        ...
    
        def get_context(self, request):
            context = super().get_context(request)
            context['form_entries'] = Form.objects.live().order_by('-your_date_field')
            return context
    

    然后在模板中使用更新后的上下文,

    {% for entry in form_entries %}
        {{ entry.title }}
    {% endfor %}
    

    【讨论】:

      【解决方案2】:

      使用 filter_form_submissions_for_user 钩子:https://docs.wagtail.io/en/stable/reference/hooks.html#filter-form-submissions-for-user

      # wagtail_hooks.py
      
      # Sort submission form list by published time
      @hooks.register('filter_form_submissions_for_user')
      def submissions_filter(user, queryset):
          return queryset.order_by("-last_published_at")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        相关资源
        最近更新 更多