Show a ManyToManyField as Checkboxes in Django Admin
Asked Answered
S

4

36

Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin? Thanks in advance!

Stereoscopic answered 24/1, 2011 at 16:45 Comment(1)
I would suggest changing the accepted answer, so as to not confuse visitors.Lollis
T
75

From this answer it seems like it is possible to use ModelAdmin.formfield_overrides to override the ManyToManyField to use CheckBoxSelectMultiple:

from django.db import models
from django.contrib import admin
from django.forms import CheckboxSelectMultiple

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.ManyToManyField: {'widget': CheckboxSelectMultiple},
    }

I haven't tried it and am merely quoting from the source, but it seems plausible. Good luck.

Warning: as @errx rightly pointed out, the documentation highlights the following caveat:

If you want to use a custom widget with a relation field (i.e. ForeignKey or ManyToManyField), make sure you haven't included that field's name in raw_id_fields or radio_fields.

formfield_overrides won't let you change the widget on relation fields that have raw_id_fields or radio_fields set. That's because raw_id_fields and radio_fields imply custom widgets of their own.

Thankful answered 24/1, 2011 at 16:57 Comment(2)
there is a warning from django docs. If you want to use a custom widget with a relation field (i.e. ForeignKey or ManyToManyField), make sure you haven't included that field's name in raw_id_fields or radio_fields. formfield_overrides won't let you change the widget on relation fields that have raw_id_fields or radio_fields set. That's because raw_id_fields and radio_fields imply custom widgets of their own.Narcoanalysis
Thanks for the heads up. Will place warning in answer.Thankful
V
7

This is most certainly possible. Here is the code which you can place in the ModelAdmin subclass:

def formfield_for_manytomany(self, db_field, request=None, **kwargs):
    if db_field.name == 'your field name':
        kwargs['widget'] = form_widgets.CheckboxSelectMultiple()
        kwargs['help_text'] = ''

    return db_field.formfield(**kwargs)

This was derived from looking into the admin code.

Valina answered 30/6, 2011 at 12:48 Comment(0)
A
5

To override for a single field the following is useful:

def get_form(self, request, obj=None, **kwargs):
    form = super(VNodeAdmin, self).get_form(request, obj, **kwargs)
    form.base_fields['node_type'].widget = forms.CheckboxSelectMultiple()
    return form
Adventuress answered 7/1, 2014 at 16:4 Comment(0)
N
0

I think there is no simple way to do this. You can try to override ModelAdmin class but this is not a "simple" way.

But you can use filter_horizontal or filter_vertical to add almost similar functionality

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal

Narcoanalysis answered 24/1, 2011 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.