如何以表格内联方法在django-admin中显示多对多字段?

时间:2019-04-10 06:11:33

标签: python django-models django-admin

我在Django中创建了两个模型,其中一个模型用于多对多字段,我想要的是在管理多对多字段中添加内容时,尽管应该将其显示在弹出窗口中内联可编辑。

我已经使用表格内联进行了尝试,但这不起作用。

    from django.db import models

    # Create your models here.


    class Author(models.Model):
       name = models.CharField(max_length=100)

    class Book(models.Model):
       author = models.ManyToManyField(Author,through=Test1.tests2.through)
       title = models.CharField(max_length=100)





from django.contrib import admin
from basic_app.models import Author,Book

# Register your models here.


class BookInline(admin.TabularInline):
    model = Book.author.through
    extra = 1

class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

admin.site.register(Book)
admin.site.register(Author,AuthorAdmin)

0 个答案:

没有答案