I’m using Wagtail’s ChooserViewSet to display a widget in the admin form. The widget uses an external API to get available questions. I want to filter these questions by passing a parameter dataset_id to the queryish model.
To do that, I’ve been following Wagtail’s documentation on linked_fields (https://docs.wagtail.org/en/stable/extending/generic_views.html#limiting-choices-via-linked-fields). The problem is, that the field is inside a streamfield. Sample code snippet below:
class MyPage(Page):
content = StreamField(
[
('questions', QuestionBlock())
],
verbose_name=_('Content'),
blank=True,
use_json_field=True,
)
content_panels = (
Page.content_panels
+ [
FieldPanel('content', classname='collapsible')
]
)
class QuestionBlock(StructBlock):
dataset_id = CharBlock()
questions = ChoiceBlock(
widget=QuestionChooserWidget(
linked_fields={
'dataset_id': {'match': r'???????????', 'append': 'dataset_id'}
}
)
)
class QuestionChooserViewSet(ChooserViewSet):
model = Question
choose_one_text = 'Choose a question.'
choose_another_text = 'Choose another question.'
url_filter_parameters = ["dataset_id"]
preserve_url_parameters = ["multiple", "dataset_id"]
QuestionChooserViewSet = QuestionChooserViewSet('question_chooser')
QuestionChooserWidget = QuestionChooserViewSet.widget_class
Do you know how I can construct a proper regex to get this dataset_id?
In the Wagtail docs it’s slightly mentioned but doesn’t elaborate on it in any way. (https://docs.wagtail.org/en/stable/extending/generic_views.html#limiting-choices-via-linked-fields)
PersonChooserWidget(linked_fields={
'country': {'selector': '#id_country'} # equivalent to 'country': '#id_country'
})
# Look up by ID
PersonChooserWidget(linked_fields={
'country': {'id': 'id_country'}
})
# Regexp match, for use in StreamFields and InlinePanels where IDs are dynamic:
# 1) Match the ID of the current widget's form element (the PersonChooserWidget)
# against the regexp '^id_blog_person_relationship-d+-'
# 2) Append 'country' to the matched substring
# 3) Retrieve the input field with that ID
PersonChooserWidget(linked_fields={
'country': {'match': r'^id_blog_person_relationship-d+-', 'append': 'country'},
})
Is there any specific logic to construct this **r’^id_blog_person_relationship-d+-‘ **thing???
Hubert Kędziora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.