Ama*_*thy 5 python django django-forms django-admin django-modeladmin
我想在 Django 管理 list_display 页面中添加可编辑的 Many2Many 字段。模型结构如下,
class Genre(models.Model):
name = models.CharField(max_length=250, unique=True)
class Movie(models.Model):
name = models.CharField(max_length=250)
genre = models.ManyToManyField(Genre)
Run Code Online (Sandbox Code Playgroud)
我尝试过,
class MovieAdmin(admin.ModelAdmin):
list_display = ['name', 'genre']
list_editable = ['genre']
Run Code Online (Sandbox Code Playgroud)
上面抛出一个错误。
小智 3
默认情况下,Django 不允许在 ModelAdmin 的 list_editable 中添加 ManyToManyField。所以我们需要重写模型管理方法。
在查看模型时,您需要按照以下步骤操作,才能在列表显示页面中获得可编辑的 ManyToManyField。
在apps/forms.py中 ,您需要定义需要在列表显示页面中编辑哪些 ManyToMany 字段。如下,
from django import forms
from app.models import Genre
class MovieChangeListForm(forms.ModelForm):
# here we only need to define the field we want to be editable
genre = forms.ModelMultipleChoiceField(queryset=Genre.objects.all(),
required=False)
Run Code Online (Sandbox Code Playgroud)
在app/admin.py中 ,您需要重写模型管理的方法。如下,
from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from app.models import Movie
from app.forms import MovieChangeListForm
class MovieChangeList(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(MovieChangeList, 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 MovieAdmin
self.list_display = ['action_checkbox', 'name', 'genre']
self.list_display_links = ['name']
self.list_editable = ['genre']
class MovieAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return MovieChangeList
def get_changelist_form(self, request, **kwargs):
return MovieChangeListForm
admin.site.register(Movie, MovieAdmin)
Run Code Online (Sandbox Code Playgroud)
现在您已经准备好检查更改,运行服务器并检查电影模型的 django admin。您可以直接从列表显示页面编辑ManyToMany字段。
注意:如果您要在列表中使用多个可编辑的ManyToManyFields,则需要在settings.py中设置 DATA_UPLOAD_MAX_NUMBER_FIELDS 。