简体   繁体   中英

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

I got some models in models.py like this:

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"""

(There are some differences between reading and writing ,but I omit that.)

and in 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,
    ]

I found that in question package admin page's inline form, both Reading and Writing shows like Question , I can't tell one from another. and they have no edit button.

What should I do to let admin shows subclass model in this situation? 在此处输入图片说明

Then in this case the super class Question should be an abstract 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

More info here Django Admin Interface Does Not Use Subclass's __unicode__()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM