Home > Software engineering >  IntegrityError - NOT NULL constraint failed
IntegrityError - NOT NULL constraint failed

Time:03-05

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?')

Hey, sorry if this is a simple issue, new to Django and any development like this. When doing this, it gives me the error:

IntegrityError at /teacher/tests NOT NULL constraint failed: main_questions.Q1

Anyone able to help me out? Willing to provide any information you would like.

CodePudding user response:

Change this line in views.py teachertests function:

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

To:

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

Paste the error codes here, if unresolved.

  • Related