Multiple select in wagtail admin
Asked Answered
S

1

8

When using Django ModelAdmin, I can use:

filter_horizontal = ('some_many_to_many_field',) 

So that, instead of showing the default multiple select widget, it shows a nice interface with two blocks for selecting some values.

Is there a similar option for using the same widget as in django ModelAdmin for my many-to-many fields in wagtail's ModelAdmin?

Thank you!

Sepaloid answered 23/3, 2017 at 20:14 Comment(0)
F
13

In answer to your question: No, there is no equivalent of filter_horizontal in Wagtail's contrib.modeladmin app.

However, there are ways to override the widget used for fields on your model. The easiest of which is to use FieldPanel's widget argument in your model's panel definitions. For example:

from django import forms
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel

class TestModel(models.Model):
    manytomany = models.ManyToManyField('someapp.SomeModel', blank=True)

    panels = [
        FieldPanel('manytomany', widget=forms.CheckboxSelectMultiple)
    ]

Or, if the model you're registering is a subclass of wagtail.core.models.Page, the following example is more appropriate:

from django import forms
from django.db import models
from modelcluster.fields import ParentalManyToManyField
from wagtail.core.models import Page
from wagtail.admin.edit_handlers import FieldPanel

class TestPageModel(Page):
    manytomany = ParentalManyToManyField('someapp.SomeModel', blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('manytomany', widget=forms.CheckboxSelectMultiple)
    ]

There is no equivalent of Django's filter_horizontal widget that can easily be used in this way though, unfortunately. That particular widget is dependant on various styles and scripts that are loaded as part of Django's admin UI. Wagtail's admin UI is completely custom and doesn't include any such styles or scripts.

Flanna answered 30/3, 2017 at 20:14 Comment(2)
Thank you for the answer. Maybe I will try to implement it myself. It'll be very useful for my clients...Sepaloid
Would MultipleChoiceBlock help solve the problem at all?Odessa

© 2022 - 2024 — McMap. All rights reserved.