About Reverse M2M Relations in Administration

If a Book has a ManyToManyField pointing to Author, then you can get a M2M selection of books at the author administration with a custom form:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# books/admin.py
from django.contrib import admin
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.utils.translation import gettext_lazy as _
from django import forms
from .models import Author, Book

class AuthorForm(forms.ModelForm):
    books = forms.ModelMultipleChoiceField(
        Book.objects.all(),
        widget=FilteredSelectMultiple(_("Books"), False),
        required=False,
    )

    class Meta:
        model = Author
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if self.instance.pk:
            self.initial[
                "books"
            ] = self.instance.book_set.values_list("pk", flat=True)

    def _save_m2m(self):
        super()._save_m2m()
        self.instance.book_set.clear()
        self.instance.book_set.add(*self.cleaned_data["books"])

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    form = AuthorForm
    fields = ("name", "books")

Tips and Tricks Programming Django 4.2 Django 3.2