我有以下型号:

class Question(models.Model):
    name = models.CharField()

class Questionpart(models.Model):
    TYPES = (
        ('TX', 'Text'),
        ('AN', 'Answer'),
    )
    question = models.ForeignKey(Question, null=True, blank=True)
    type = models.CharField(max_length=2, choices=TYPES) # defines which QuestionPart_ will be used as additional data
    step_number = models.IntegerField()

class Questionpart_answer(models.Model):
    questionpart = models.ForeignKey(Questionpart)
    #other answer specific fields

class Questionpart_text(models.Model):
    questionpart = models.ForeignKey(Questionpart)
    #other text specific fields, there are a few more like this

我已经有一个处理添加/编辑问题的表单,但每个问题都由几个文本/答案(以及其他类型,这里省略)组成 . 我希望能够在编辑问题时添加/删除步骤(在一个页面上) .

我已经创建了一个formset - QuestionPartFormset 并且在 QuestionPartModelForm 中我根据类型将相应模型中的字段添加到此表单中 . 这看起来真的很难看 . 一定有更好的方法!!

我正在考虑一个 inline_formsets 的形式...我会感激任何建议!我的模型定义好吗?不应该从 QuestionPart 模型继承每个类型,例如:(我认为Django在这种情况下也会创建一个外键,但至少我会有一个超类):

class QuestionPart_answer(QuestionPart):