A
A
astatium1352021-03-11 07:11:45
Django
astatium135, 2021-03-11 07:11:45

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

and
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)

604997ba5c729321253533.jpeg
The task is to make it possible not only to select, but also to create / edit related records.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-03-11
@bacon

So for "create/edit related posts" you have two icons in the picture, after the dropdown list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question