Show a ManyToManyField as Checkboxes in Django Admin(在 Django Admin 中将 ManyToManyField 显示为复选框)
问题描述
Is there a simple way to show a ManyToManyField as Checkboxes in Django Admin? Thanks in advance!
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.
这篇关于在 Django Admin 中将 ManyToManyField 显示为复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Django Admin 中将 ManyToManyField 显示为复选框
基础教程推荐
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01