如何在 Django-admin 内联表单中显示子类模型?

How to show subclass model in Django-admin inline form?

我在 models.py 中得到了一些这样的模型:

class QuestionPackage(models.Model):
    """Question Package"""


class Question(models.Model):
    question_package = models.ManyToManyField(QuestionPackage, blank=True)
    name = models.CharField(max_length=128)
    answer = models.TextField()


class Reading(Question):
    """Reading Question"""


class Writing(Question):
    """Writing Question"""

(读和写有一些区别,我忽略了。)

并在 admin.py 中:

class ReadingInline(admin.TabularInline):
    model = Reading.question_package.through


class WritingInline(admin.TabularInline):
    model = Writing.question_package.through


class QuestionPackageAdmin(admin.ModelAdmin):
    inlines = [
        ReadingInline, WritingInline,
    ]

我发现在question package管理页面的内联表单中,ReadingWriting都显示为Question,我无法区分它们。他们没有编辑按钮。

在这种情况下,如何让管理员显示子类模型?

那么在这种情况下,超级 class 问题应该是抽象的 class:

class Question(models.Model):
    question_package = models.ManyToManyField(QuestionPackage, blank=True)
    name = models.CharField(max_length=128)
    answer = models.TextField()

    class Meta:
        abstract = True

https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes

这里有更多信息Django Admin Interface Does Not Use Subclass's __unicode__()