Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin? Thanks in advance!
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.
The attribute prepopulated_fields tells the admin application to automatically fill the field slug - in this case with the text entered into the name field.
To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).
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 inraw_id_fields
orradio_fields
.
formfield_overrides
won't let you change the widget on relation fields that haveraw_id_fields
orradio_fields
set. That's becauseraw_id_fields
andradio_fields
imply custom widgets of their own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With