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

时间:2015-04-24 08:38:14

标签: python django django-models django-admin

我在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,我无法告诉彼此。他们没有编辑按钮。

在这种情况下,我该怎么办才能让admin显示子类模型? enter image description here

1 个答案:

答案 0 :(得分:1)

然后在这种情况下,超类问题应该是一个抽象类:

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__()