/ project / new /中的IntegrityError

时间:2018-09-04 09:05:39

标签: python django

我正在尝试为django应用创建项目创建部件。但我收到此错误:

/ project / new /

中的IntegrityError

没有NULL约束失败:project_project.author_id

这是我的观点:

view.py

def project_new(request):
    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            project = form.save()
            project.author = request.user
            project.published_date = timezone.now()
            project.save()
            return redirect('blog/project_detail.html', pk=project.pk)
    else:
        form = ProjectForm()
    return render(request, 'blog/project_new.html', {'form': form})

这是我的模特 models.py

class Project(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = RichTextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

这是我的表单文件 forms.py

class ProjectForm(forms.ModelForm):
    first_name = forms.CharField(max_length=30, required=True, help_text='Required.')
    last_name = forms.CharField(max_length=30, required=True, help_text='Required.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
    project_name = forms.CharField(max_length=30, required=True, help_text='Required')
    project_description = forms.CharField(max_length=10000, required=True, help_text='Required.')
    project_notes = forms.CharField(max_length=1000, required=False, help_text='Optional.')

    class Meta:
        model = Project
        fields = ('first_name', 'last_name', 'email', 'project_name',
                  'project_description', 'project_notes')

这是mt html文件。我不确定,但是在这一部分,我不确定“按钮”代码。我可能是错的 project_detail.html

<form method="POST" style="text-align: center" >
    {% csrf_token %}
    <input type='hidden' value='m3KqcAqxJtXsJLS8ZRfhlXvTiXc1G0K0Whojb71JSe4jDsHYPwuOWKPIlFELyGoQ'/>

    <p>
        <label for="id_first_name">name</label><br>
        <input type="text" name="first_name" required id="id_first_name" maxlength="30"/>

    </p>

    <p>
        <label for="id_last_name">last name:</label><br>
        <input type="text" name="last_name" required id="id_last_name" maxlength="30"/>

    </p>

    <p>
        <label for="id_email">Email:</label><br>
        <input type="email" name="email" required id="id_email" maxlength="254"/>

    </p>

    <p>
        <label for="id_project_name">Project name:</label><br>
        <input type="text" name="project_name" required id="id_project_name" maxlength="30"/>

    </p>

    <p>
        <label for="id_project_description">Project description:</label><br>
        <input type="text" name="project_description" required id="id_project_description" maxlength="10000"/>

    </p>

    <p>
        <label for="id_project_notes">Proje notes:</label><br>
        <input type="text" name="project_notes" id="id_project_notes" maxlength="1000"/>

    </p>

    <button style="color: #9C1A1C; font-weight: bold" type="submit" class="save btn btn-default">Save</button>

</form>

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

您的表单ProjectForm应该是forms.ModelForm的子类,而不是forms.Form

class ProjectForm(forms.ModelForm):
    first_name = forms.CharField(max_length=30, required=True, help_text='Required.')
    last_name = forms.CharField(max_length=30, required=True, help_text='Required.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
    project_name = forms.CharField(max_length=30, required=True, help_text='Required')
    project_description = forms.CharField(max_length=10000, required=True, help_text='Required.')
    project_notes = forms.CharField(max_length=1000, required=False, help_text='Optional.')

    class Meta:
        model = Project
        fields = ('first_name', 'last_name', 'email', 'project_name',
                  'project_description', 'project_notes')

更新

要添加不可为空的author的{​​{1}},应在将更改提交到数据库之前添加作者对象。

ForeignKey