我有一个TestingQuestion和TestingQuestionOption类。 在创建TestingQuestions时,我也使用stackinline来创建TestingQuestionOption。 目前创建新的TestingQuestion以及TestingQuestionOptions工作。 我遇到的问题是当我想保存我已经创建的TestingQuestion时。即尝试编辑并保存更改。 我在这一行上得到“list index out of range”错误
"question = saved_options[0].question"
位于“TestingQuestionFormSet”类
下的“save_existing_objects”方法中saved_options = super(TestingQuestionFormSet, self).save_new_objects(commit)
返回None,我不知道为什么,因为我期望它返回TestingQuestionOption。 任何帮助都将大大鼓励。感谢
这是代码
models.py
class TestingQuestion(models.Model):
class TestingQuestionOption(models.Model):
question = models.ForeignKey(TestingQuestion, null=True, blank=False)
admin.py
class TestingQuestionOptionInline(admin.StackedInline, SummernoteInlineModelAdmin):
model = TestingQuestionOption
formset = TestingQuestionFormSet
class TestingQuestionAdmin(SummernoteModelAdmin, ImportExportModelAdmin):
form = TestingQuestionCreateForm
inlines = (TestingQuestionOptionInline,)
forms.py
class TestingQuestionFormSet(forms.models.BaseInlineFormSet):
def save_new_objects(self, commit=True):
saved_options = super(TestingQuestionFormSet, self).save_new_objects(commit)
if commit:
question = saved_options[0].question
for option in saved_options:
if option.order == 0:
option.order = TestingQuestionOption.objects.filter(question=question).count() + 1
if option.name == "Auto Generated":
option.name = "%s Option %s" % (question, option.order)
option.save()
if len(saved_options) < 2:
order = TestingQuestionOption.objects.filter(question=question).count() + 1
name = "%s Option %s" % (question, order)
TestingQuestionOption.objects.create(name=name, order=order, question=question, correct=False)
return saved_options
def save_existing_objects(self, commit=True):
saved_options = super(TestingQuestionFormSet, self).save_existing_objects(commit)
if commit:
question = saved_options[0].question
for option in saved_options:
if option.order == 0:
option.order = TestingQuestionOption.objects.filter(question=question).count() + 1
if option.name == "Auto Generated":
option.name = "%s Option %s" % (question, option.order)
option.save()
if len(saved_options) < 2:
order = TestingQuestionOption.objects.filter(question=question).count() + 1
name = "%s Option %s" % (question, order)
TestingQuestionOption.objects.create(name=name, order=order, question=question, correct=False)
return saved_options
class TestingQuestionCreateForm(forms.ModelForm):
def save(self, commit=True):
testing_question = super(TestingQuestionCreateForm, self).save(commit=False)
if testing_question.order == 0 or testing_question.name == "Auto Generated":
count = TestingQuestion.objects.filter(module__id=self.data.get("module")).count() + 1
module = Module.objects.get(id=self.data.get("module"))
name = "%s Question %d" % (module, count)
while TestingQuestion.objects.filter(name=name):
count += 1
name = "%s Question %d" % (module, count)
testing_question.name = name
testing_question.order = count
testing_question.save()
question_content = self.cleaned_data.get("question_content")
testing_question.question_content = process_mathml_content(question_content, 0, testing_question.id)
answer_content = self.cleaned_data.get("answer_content")
testing_question.answer_content = process_mathml_content(answer_content, 1, testing_question.id)
testing_question.save()
return testing_question
class Meta:
model = TestingQuestion