Skip to content Skip to sidebar Skip to footer

How To Edit Manytomanyfield In Django Admin List Display Page?

I want to add Many2Many fields editable in Django admin list_display page. The model structure as below, class Genre(models.Model): name = models.CharField(max_length=250, uniq

Solution 1:

Django by default won't allow to add ManyToManyField in list_editable in ModelAdmin. So we need to override model admin methods.

On looking your models you need to follow below steps to get the ManyToManyField editable in list display page.

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 Genre


classMovieChangeListForm(forms.ModelForm):

    # here we only need to define the field we want to be editable
    genre = forms.ModelMultipleChoiceField(queryset=Genre.objects.all(), 
        required=False)

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 Movie
from app.forms import MovieChangeListForm

classMovieChangeList(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']


classMovieAdmin(admin.ModelAdmin):

    defget_changelist(self, request, **kwargs):
        return MovieChangeList

    defget_changelist_form(self, request, **kwargs):
        return MovieChangeListForm


admin.site.register(Movie, MovieAdmin)

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.

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 .

Solution 2:

As far as I understand, you want to add the model in your admin page.

to do so, you need to simply do this in your admin.py file inside your django app

admin.site.register(MovieAdmin)

It is automatically editable. Hope this helps.

Post a Comment for "How To Edit Manytomanyfield In Django Admin List Display Page?"