Reputation: 155
I am having trouble figuring out how to implement the wagtail-generic-chooser. My goal is rather straightforward. I have a lot of data that is not hierarchical so it does not make sense to create the data as Pages. Wagtail Model Admin is the perfect solution. However, I need to be able to cross-reference the data between models and ideally it would be perfect to have a Chooser Panel much like PageChooserPanel or SnippetChooserPanel when selecting the data. However, there is no modelchooserpanel built into wagtail. I tried many of the 3rd party apps that attempt to accomplish this but they do not work with newer versions of wagtail or with python 3. Finally I came across wagtail-generic-chooser and it appears to be the perfect flexible solution that will allow me to create a snippetChooserPanel type selector for model connection I need.
Unfortunately, I am running into issues I have followed the implementation instructions outlined in the documentation, but I am not clear on what everything is nor how it should be filled out.
https://github.com/wagtail/wagtail-generic-chooser#chooser-widgets-model-based
I have an event model referencing an event category model. Both the event category model and the event model are set up as modelAdmin elements.
I installed wagtail-generic-chooser and added it to my installed apps.
My code
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)))
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"
In the widget.py file, where does the 'choose_modal_url_name' value come from? I renamed it from people_chooser as in the docs example above to one that was relevant to my class but it seems to be causing an error. Do I need to register this value somewhere? Do I need to register the widget? Or does this value come from somewhere specific. Currently, It throws an error:
NoReverseMatch at /admin/events/eventoverview/edit/1/ 'event_chooser' is not a registered namespace
For this function where do I get the values for the reverse function. I am not reference a wagtailsnippet but instead a wagtailmodel so how do I reference that? What do I use in the args tuple?
def get_edit_item_url(self, item):
#question???
return reverse('wagtailsnippets:edit', args=('categories', 'CategoryEventCollection', quote(item.pk)))
Any advice or direction you can provide would be much appreciated. Or if you can let me know what I need to make this modelchooserpanel functional.
Upvotes: 0
Views: 763
Reputation: 155
For anyone else trying to get this working who may be having trouble.
Step 1. Set up the chooser view in views.py,
Step 2. register the viewset in the wagtail_hooks.py,
Step 3. create the AdminChooser class that references the registered viewset by name, you gave it in the return function.
Step 4. import and use as a widget on the FieldPanel you want.
Upvotes: 0