Answer the question
In order to leave comments, you need to log in
How to edit related m2m models in Django admin?
There is a code of the form (specifically, this one is taken from the example on the official site):
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Publication(models.Model):
title = models.CharField(max_length=30)
class Meta:
ordering = ['title']
def __str__(self):
return self.title
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
class Meta:
ordering = ['headline']
def __str__(self):
return self.headline
from django.contrib import admin
from .models import Publication, Article
class MembershipInline(admin.TabularInline):
model = Article.publications.through
class ArticleAdmin(admin.ModelAdmin):
exclude = ('publications', )
inlines = [MembershipInline, ]
admin.site.register(Article, ArticleAdmin)
class PublicationAdmin(admin.ModelAdmin):
inlines = [MembershipInline, ]
admin.site.register(Publication, PublicationAdmin)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question