Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django many-to-many, display in admin

Please help to understand how to display list of groups in Django admin Person instance in this case:

class Person(models.Model):
  name = models.Charfield(max_length=120)

class Group(models.Model):
  title = models.Charfield(max_length=120)
  persons = models.ManyToManyField(Person)
like image 232
Karen Galstyan Avatar asked Nov 27 '25 05:11

Karen Galstyan


1 Answers

You need to use inlines. Add this to the admin.py file:

from .models import Group

class GroupInline(admin.TabularInline):
    model = Group.persons.through
    extra = 1

class PersonAdmin(admin.ModelAdmin):
    inlines = [GroupInline]

admin.site.register(Person, PersonAdmin)

See details here.

like image 57
neverwalkaloner Avatar answered Nov 28 '25 20:11

neverwalkaloner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!