Hi folks – I have essentially the following model, plus the default Group
model, so a study can be accessed by many groups and each group can give access to many studies:
# models.py
from django.contrib.auth.models import Group
from django.db import models
class Study(models.Model):
groups = models.ManyToManyField(Group, blank=True)
Together with a small amount of configuration, this shows the fancy filter_horizontal
widget on the admin page for each study:
# admin.py
from django.contrib import admin
from .models import Study
@admin.register(Study)
class StudyAdmin(admin.ModelAdmin):
filter_horizontal = ["groups"]
I’d like to show the same widget on the admin page for each group, but I can’t figure out how to do that. I can’t find a value to put in GroupAdmin.filter_horizontal
that won’t result in an error like “The value of ‘filter_horizontal[0]’ must be a many-to-many field.” or “The value of ‘filter_horizontal[0]’ refers to ‘study_set’, which is not a field of ‘auth.Group’.”, and trying to use the officially-documented approach with inlines by doing something like this:
class StudyGroupInline(admin.TabularInline):
model = Study.groups.through
admin.site.unregister(Group)
@admin.register(Group)
class GroupAdmin(BaseGroupAdmin):
inlines = [StudyGroupInline]
results in this bizarre-looking widget instead:
I can specify StudyGroupInline.extra = 1
to get rid of some of the additional rows, but it’s still a far cry from the nice-looking two-column widget you get by default. What am I missing?
I’m using Django 5.1.3 and Python 3.12.7.