Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple select in wagtail admin

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!

like image 311
DSLima90 Avatar asked Mar 23 '17 20:03

DSLima90


Video Answer


1 Answers

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.

like image 167
ababic Avatar answered Oct 16 '22 01:10

ababic