问题描述
在Django管理员中,我非常希望能够在列表显示中显示可编辑的ManyToManyField.
In the Django admin, I would really like to be able to display an editable ManyToManyField in the list display.
不一定要使用完整的ManyToManyField控件-仅保存一个值就足以用于列表显示(尽管基础值本质上是多对多).
It doesn't necessarily need to be the full ManyToManyField control - being able to save just one value would be good enough for the purposes of the list display (though the underlying values are many-to-many in nature).
我的模型如下:
class Item(models.Model):
name = models.CharField(max_length=500)
colour = models.ManyToManyField(Colour, related_name='primary_colour')
如果我在admin.py
中尝试此操作:
If I try this in admin.py
:
class ItemAdmin(admin.ModelAdmin):
list_display = ('name', 'colour')
list_editable = ('colour')
然后我收到此错误:
'ItemAdmin.list_display[6]', 'colour' is a ManyToManyField which is not supported.
我可以以任何方式在列表显示中显示可编辑的ManyToManyField以进行快速编辑吗?
Is there any way at all that I can show an editable ManyToManyField for rapid editing in the list display?
我发现了一个相关的问题,它解释了如何使值在列表显示中可见,但不可
I found this related question, which explains how to make the values visible in the list display, but not editable: ManyToManyField widget in a django admin change list?
推荐答案
默认情况下,Django不允许在ModelAdmin的list_editable中添加ManyToManyField.因此,我们需要重写模型管理方法.
Django by default won't allow to add ManyToManyField in list_editable in ModelAdmin. So we need to override model admin methods.
在查看模型时,您需要按照以下步骤操作,以便在列表显示页面中编辑ManyToManyField.
On looking your models you need to follow below steps to get the ManyToManyField editable in list display page.
在 apps/forms.py 中,您需要定义需要在列表显示页面中进行编辑的ManyToMany字段.如下,
In apps/forms.py you need to define which ManyToMany fields you need to make editable in list display page. As below,
from django import forms
from app.models import Item
class ItemChangeListForm(forms.ModelForm):
# here we only need to define the field we want to be editable
colour = forms.ModelMultipleChoiceField(queryset=Colour.objects.all(),
required=False)
在 app/admin.py 中,您需要覆盖模型admin的方法.如下,
In app/admin.py you need to override methods of model admin. As below,
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from app.models import Item
from app.forms import ItemChangeListForm
class ItemChangeList(ChangeList):
def __init__(self, request, model, list_display,
list_display_links, list_filter, date_hierarchy,
search_fields, list_select_related, list_per_page,
list_max_show_all, list_editable, model_admin):
super(ItemChangeList, self).__init__(request, model,
list_display, list_display_links, list_filter,
date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable,
model_admin)
# these need to be defined here, and not in ItemAdmin
self.list_display = ['action_checkbox', 'name', 'colour']
self.list_display_links = ['name']
self.list_editable = ['colour']
class ItemAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return ItemChangeList
def get_changelist_form(self, request, **kwargs):
return ItemChangeListForm
admin.site.register(Item, ItemAdmin)
现在,您都准备检查更改,运行服务器并检查django admin的电影模型.您可以直接从列表显示页面编辑ManyToMany字段.
Now you all set to check the changes, run server and check django admin for Movie model. You can edit ManyToMany field directly from list display page.
注意:如果您打算使用列表中可编辑的多目标多对多字段,则需要在设置中设置DATA_UPLOAD_MAX_NUMBER_FIELDS .py .
Note : If you are going to use muliptle ManyToManyFields editable in list then, you need to set DATA_UPLOAD_MAX_NUMBER_FIELDS in settings.py .
这篇关于Django管理员:list_editable中的ManyToManyField吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!