如何为多种类型的问题创建django管理内联?
我有一个模型,Challenge_Question:
class ChallengeQuestion(models.Model):
challenge = models.ForeignKey('Challenge')
question = models.CharField(max_length=500)
displayed_answers = models.IntegerField()
required = models.BooleanField()
multiple_choice = 'MC'
fill_in_the_blank = 'FB'
ordering = 'OR'
true_false = 'TF'
question_choices = (
(multiple_choice, 'Multiple Choice'),
(fill_in_the_blank, 'Fill In The Blank'),
(ordering, 'Ordering'),
(true_false, 'True/False'),
)
question_type = models.CharField(max_length=2, choices=question_choices, default=multiple_choice, db_index=True)
对于每个question_type,然后我有一个相关的模型FK到ChallengeQuestion:
class ChallengeQuestionMC(models.Model): #for multiple choice questions
question = models.ForeignKey('ChallengeQuestion')
option = models.CharField(max_length=500)
is_answer = models.BooleanField()
class ChallengeQuestionFB(models.Model): #for fill-in-the-blank questions
question = models.ForeignKey('ChallengeQuestion')
option = models.CharField(max_length=100)
sequence = models.SmallIntegerField(default=1, blank=True, null=True)
class ChallengeQuestionTF(models.Model): #for true/false questions
question = models.ForeignKey('ChallengeQuestion')
is_correct = models.BooleanField()
class ChallengeQuestionOR(models.Model): #for ordering type questions
question = models.ForeignKey('ChallengeQuestion')
option = models.CharField(max_length=500)
sequence = models.SmallIntegerField(default=1, blank=True, null=True)
我希望接下来要做的是为每个问题提供适当的管理内嵌选项,具体取决于question_type。
例如,如果我填写了True / False类型的challenge_question,我希望可以使用True / False“is_correct”字段来检查或不检查。 如果问题是订购类型,我希望订单选项可以与顺序字段一起提供。 等等。
如果我只是添加到admin.py:
class ChallengeQuestionOptionsInline(admin.StackedInline):
model = ChallengeQuestionMC
extra = 2
class ChallengeQuestionAdmin(admin.ModelAdmin):
inlines = [ChallengeQuestionOptionsInline]
admin.site.register(ChallengeQuestion, ChallengeQuestionAdmin)
那么这显然不会按照我想要的方式工作。 我需要在这里有一些条件逻辑,例如“如果challenge_question是question_type'MC',那么使用ChallengeQuestionMC模型,依此类推。
我无法找到任何关于这种条件逻辑如何在django管理员内联中运行的任何建议?
此代码未经测试,因此可能需要调整。 然而,在我看来可能的是,不是在整个ModelAdmin中定义全局内联,而是通过扩展change_view函数(这是管理系统用于更改特定对象的视图)实际上可以在本地设置它们。 它可能看起来像这样:
class MCInline(admin.StackedInline):
model = ChallengeQuestionMC
class FBInline(admin.StackedInline):
model = ChallengeQuestionFB
class TFInline(admin.StackedInline):
model = ChallengeQuestionTF
class ORInline(admin.StackedInline):
model = ChallengeQuestionOR
class ChallengeQuestionAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
model = self.model
obj = self.get_object(request, unquote(object_id))
if obj.question_type == 'MC'
self.inlines = [MCInline]
elif obj.question_type == 'FB'
self.inlines = [FBInline]
elif obj.question_type == 'TF'
self.inlines = [TFInline]
elif obj.question_type == 'OT'
self.inlines = [ORInline]
return super(MyModelAdmin, self).change_view(request, object_id,
form_url, extra_context=extra_context)
再次,这是未经测试的,所以很可能需要一些调整才能工作。
链接地址: http://www.djcxy.com/p/65427.html上一篇: How to create django admin inlines for multiple types of questions?
下一篇: django: more models with the same fields and admin interface