我在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
管理页面的内联表单中,Reading
和Writing
都显示为Question
,我无法告诉彼此。他们没有编辑按钮。
在这种情况下,我该怎么办才能让admin显示子类模型?
答案 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__()