0

我无法弄清楚如何实现 wagtail-generic-chooser。我的目标相当简单。我有很多不是分层的数据,因此将数据创建为页面是没有意义的。Wagtail Model Admin 是完美的解决方案。但是,我需要能够在模型之间交叉引用数据,理想情况下,在选择数据时,最好有一个类似于 PageChooserPanel 或 SnippetChooserPanel 的选择器面板。但是,鹡鸰中没有内置模型选择器面板。我尝试了许多试图实现此目的的第 3 方应用程序,但它们不适用于较新版本的 wagtail 或 python 3。最后我遇到了 wagtail-generic-chooser,它似乎是一个完美的灵活解决方案,可以让我为我需要的模型连接创建一个 snippetChooserPanel 类型选择器。

不幸的是,我遇到了我遵循文档中概述的实施说明的问题,但我不清楚所有内容是什么,也不应该如何填写。

https://github.com/wagtail/wagtail-generic-chooser#chooser-widgets-model-based

我有一个引用事件类别模型的事件模型。事件类别模型和事件模型都设置为 modelAdmin 元素。

我安装了 wagtail-generic-chooser 并将其添加到我安装的应用程序中。

我的代码

类别.widget.py

from django.contrib.admin.utils import quote
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

from generic_chooser.widgets import AdminChooser

from categories.models import CategoryEventCollection

class EventChooser(AdminChooser):
    choose_one_text = _('Choose a Event')
    choose_another_text = _('Choose another Event')
    link_to_chosen_text = _('Edit this Event')
    model = CategoryEventCollection

    #question???
    choose_modal_url_name = 'event_chooser:choose'

def get_edit_item_url(self, item):

    #question???
    return reverse('wagtailsnippets:edit', args=('categories', 'CategoryEventCollection', quote(item.pk)))

事件模型 events.models.py 中的用法

from django.db import models
from wagtail.admin.edit_handlers import MultiFieldPanel, FieldPanel, PageChooserPanel
from wagtail.core.fields import RichTextField

from wagtail.images.edit_handlers import ImageChooserPanel
from modelcluster.models import ClusterableModel
from categories.widgets import EventChooser


class EventOverview(ClusterableModel):

    template = 'events/event_overview_page'

    collection = models.ForeignKey(
        "categories.CategoryEventCollection",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+"
    )
    event_title = models.CharField(max_length=120, blank=True, null=True)
    event_descriptor = models.TextField(blank=True, null=True)

    panels = [

        FieldPanel("collection", widget=EventChooser),
        FieldPanel("event_title"),
        FieldPanel("event_descriptor"),

    ]


    class Meta:
        verbose_name = "Event Overview"
        verbose_name_plural = "Event Overviews"

在 widget.py 文件中,'choose_modal_url_name' 值从何而来?我将它从上面的文档示例中的 people_chooser 重命名为与我的课程相关的一个,但它似乎导致了错误。我需要在某处注册这个值吗?我需要注册小部件吗?或者这个值是否来自某个特定的地方。目前,它会引发错误:

/admin/events/eventoverview/edit/1/ 的 NoReverseMatch 'event_chooser' 不是注册的命名空间

对于这个函数,我从哪里获得反向函数的值。我不是参考 wagtailsnippet 而是参考 wagtailmodel 那么我该如何参考呢?我在 args 元组中使用什么?

def get_edit_item_url(self, item):

    #question???
    return reverse('wagtailsnippets:edit', args=('categories', 'CategoryEventCollection', quote(item.pk)))

您可以提供的任何建议或指导将不胜感激。或者如果你能让我知道我需要什么来使这个模型选择器面板正常工作。

4

1 回答 1

0

对于任何其他试图让这个工作可能遇到麻烦的人。

Step 1. 在views.py中设置选择器视图,

步骤 2. 在 wagtail_hooks.py 中注册视图集,

步骤 3. 创建通过名称引用注册视图集的 AdminChooser 类,您在返回函数中给出了它。

步骤 4. 导入并用作所需的 FieldPanel 上的小部件。

于 2020-03-28T21:35:50.300 回答