IntegrityError - NOT NULL 约束失败

IntegrityError - NOT NULL constraint failed

def teachertests(request):
    form = InitialForm()
    if request.method == "POST":
            form = InitialForm(request.POST)
            if form.is_valid():
                data_form = form.cleaned_data
                question = Questions() 
                question.test_label = data_form.get('Test_Name')
                question.Q1 = data_form.get('Q1')
                question.Topic1 = data_form.get('Topic1')
                question.Option1_Q = data_form.get('Option1_Q')
                question.Option1_Q = data_form.get('Option2_Q')
                question.Option1_Q = data_form.get('Option3_Q')
                question.Option1_Q = data_form.get('Option4_Q')
                question.save()
    return render(request, 'teachertests.html', {'form':form})

class Questions(models.Model):
    testID = AutoSlugField(unique=True)
    test_label = models.CharField(max_length=1000)

    Q1 = models.CharField(max_length=1000)
    Topic1 = models.CharField(max_length=1000)
    Option1_Q = models.CharField(max_length=1000)
    Option2_Q = models.CharField(max_length=1000)
    Option3_Q = models.CharField(max_length=1000)
    Option4_Q = models.CharField(max_length=1000)
class InitialForm(forms.Form):
    Test_Name = forms.CharField(label='Label this test')
    Question1 = forms.CharField(label = 'What is the first question?')
    Topic1 = forms.CharField(label = 'What topic is this on?')
    Option1_Q = forms.CharField(label = 'What is the first option?')
    Option2_Q = forms.CharField(label = 'What is the second option?')
    Option3_Q = forms.CharField(label = 'What is the third option?')
    Option4_Q = forms.CharField(label = 'What is the fourth option?')

嘿,抱歉,如果这是一个简单的问题,对于 Django 和任何类似的开发都是新的。这样做时,它给了我错误:

/teacher/tests 处出现完整性错误 NOT NULL 约束失败:main_questions.Q1

有人能帮帮我吗?愿意提供任何你想要的信息。

在 views.py teachertests 函数中更改此行:

question.Q1 = data_form.get('Q1')

收件人:

question.Q1 = data_form.get('Question1')

并应用@Ash Singh 提到的步骤