[Solved]-Django Admin, accessing reverse many to many

24👍

✅

You have to use inlines in your admin pages.

in your admin.py

from __future__ import unicode_literals
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from django.contrib.auth import get_user_model
from .models import Company

User = get_user_model()

class CompanyInline(admin.TabularInline):
    model = Company.users.through


@admin.register(User)
class MyUserAdmin(UserAdmin):
    inlines = [CompanyInline, ]


@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    pass

You can read more about this in docs here

There are several options available for Inlines (for example – stacked/inline, collapse, extra, min etc) you can read more about it in the docs

👤at14

Leave a comment