IntegrityError - NOT NULL 约束失败

发布于 2025-01-11 19:04:28 字数 1923 浏览 0 评论 0原文

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 和任何类似的开发来说都是新的。这样做时,它给我错误:

IntegrityError at /teacher/tests NOT NULL 约束失败:main_questions.Q1

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-01-18 19:04:28

将views.pyteachertests函数中的这一行更改

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

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

:并应用@Ash Singh提到的步骤

Change this line in views.py teachertests function:

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

To:

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

And apply the steps mentioned by @Ash Singh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文