【问题标题】:Adding Q Lookup to Wagtail CMS Snippets将 Q 查找添加到 Wagtail CMS 片段
【发布时间】:2017-07-03 12:35:53
【问题描述】:

我正在建立一个餐厅网站,并使用 Wagtail CMS Snippets 让业主管理菜单项。菜单项列表变得相当长,我想知道是否有任何方法可以将搜索输入字段添加到 Snippets 管理窗口?下面是一个带注释的屏幕截图,以供视觉参考。谢谢。

【问题讨论】:

    标签: django django-models wagtail wagtail-snippet


    【解决方案1】:

    这可以通过使用 Wagtail 的 ModelAdmin 模块 (http://docs.wagtail.io/en/v1.8.1/reference/contrib/modeladmin/) 轻松解决,您只需将这段代码添加到您的 wagtail_hooks.py 文件中:

    from wagtail.contrib.modeladmin.options import (
        ModelAdmin, modeladmin_register)
    from .models import Product
    
    
    class ProductAdmin(ModelAdmin):
        model = Product
        menu_label = 'Product'  # ditch this to use verbose_name_plural from model
        menu_icon = 'date'  # change as required
        menu_order = 200  # will put in 3rd place (000 being 1st, 100 2nd)
        add_to_settings_menu = False  # or True to add your model to the Settings sub-menu
        exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
        list_display = ('title', 'example_field2', 'example_field3', 'live')
        list_filter = ('live', 'example_field2', 'example_field3')
        search_fields = ('title',)
    
    # Now you just need to register your customised ModelAdmin class with Wagtail
    modeladmin_register(ProductAdmin)
    

    它将为您的 Products 模型创建一个单独的菜单条目,该菜单条目可自定义,就像默认的 Django Admin 列表一样。这意味着您可以轻松地将不同的过滤器和分类器添加到列表中。

    这是一个非常强大的功能,我自己根本不会向客户展示“片段”部分;它太简单和丑陋了。相反,我为每个 sn-p 创建了一个单独的 ModelAdmin,这赋予了我自定义的能力。

    【讨论】:

    • 太棒了!!只是表明我需要进一步研究 Wagtail 文档!!
    【解决方案2】:

    一旦您将模型设置为使用搜索系统进行索引,搜索栏就会自动出现。您可以通过从 wagtail.wagtailsearch.index.Indexed 类继承并在模型上定义 search_fields 列表来做到这一点,如下所述:http://docs.wagtail.io/en/v1.8.1/topics/search/indexing.html#wagtailsearch-indexing-models

    (请注意,如果您使用的是 Elasticsearch,则还需要运行 ./manage.py update_index 将项目添加到搜索索引。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多