“ job_id”列中的空值违反了非空约束

时间:2019-07-10 12:07:09

标签: python django

我正在尝试上传pdf文件,但是我一直收到完整性错误,当我尝试提交pdf文件时,好像我的数据库中有一些空白,我不知道,也许有人请帮我!错误是:

IntegrityError at /posts/resume/
null value in column "job_id" violates not-null constraint

models.py

class Jobs(models.Model):
    title = models.CharField(max_length=80)
    jobs_type = models.CharField(max_length=80, choices=JOB_CHOICES)
    description = models.TextField()
    requirements = models.TextField()
    posted_date = models.DateTimeField(auto_now_add=True)
    start_date = models.DateField()
    deadline = models.DateField()
    link = models.URLField()
    slug = models.SlugField(max_length=150)
    contacts = models.CharField(max_length=80)
    tags = TaggableManager()

    class Meta:
        ordering = ('-posted_date',)



class Application(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, related_name="application",
        on_delete=models.CASCADE)

    job = models.ForeignKey(Jobs, on_delete=models.CASCADE)
    professional_summary = models.TextField()
    resume = models.CharField(max_length=150)
    uploaded_at = models.DateTimeField(auto_now_add=True)

form.py

 class ApplicationForm(forms.ModelForm):
        resume = forms.FileField(widget=forms.FileInput(attrs={'onchange': 'uploadPreview(this)'}))
        oss_resume = forms.CharField (widget=forms.HiddenInput(), required=False)

    class Meta:
        model = Application
        fields = ('professional_summary', 'resume', )

views.py

class CreateApplicationView(LoginRequiredMixin, CreateView):
    form_class = ApplicationForm
    model = Application
    message = _("Your Event has been created.")
    success_url = reverse_lazy('posts:list_jobs')
    def __init__(self, **kwargs):
        self.object=None
        super().__init__(**kwargs)

        def form_valid(self, form):
            resume = form.cleaned_data['oss_resume']

            form.instance.user = self.request.user
            submit = form.save()
            submit.user= self.request.user

            if not resume:
                return ('posts/no_resume.html')

            else:
                submit.save()
        def get_success_url(self):
            messages.success(self.request, self.message)
            return reverse('posts:list_events')

        def get_object(self):
            resume = kwargs.get('resume')
            return Application.objects.get(resume=resume)

urls.py

url(r'^list-jobs/$', JobsListView.as_view(), name='list_jobs'),





url(r'^resume/$', CreateApplicationView.as_view(), name='resume'),

结果是

IntegrityError at /posts/resume/
null value in column "job_id" violates not-null constraint
DETAIL:  Failing row contains (7, first job , processing.pdf, 2019-07-10 11:40:06.873356+00, null, null).

1 个答案:

答案 0 :(得分:0)

您的Application模型具有ForeignKeyJobs模型(我认为应该只是Job,但这是另一回事),但是它不能为空/空白。在我看来,您的视图没有为使用PDF上传创建的Jobs实例提供Application实例,因此出现了错误。

更新

一种实现方法是将null=True, blank=True添加到job模型的Application字段声明中。但是,这听起来不是一个好的解决方案,因为应将应用程序与工作相关联。因此,当您在Application中处理表单提交时,您需要允许用户选择CreateApplicationView表单中要申请的职位,或自动将该职位分配给应用程序。

更新2:

在您的ApplicationForm中,尝试执行以下操作:

 class ApplicationForm(forms.ModelForm):
        resume = forms.FileField(widget=forms.FileInput(attrs={'onchange': 'uploadPreview(this)'}))
        oss_resume = forms.CharField(widget=forms.HiddenInput(), required=False)
        job = forms.ModelChoiceField(queryset=Jobs.objects.all())


    class Meta:
        model = Application
        fields = ('professional_summary', 'resume', 'job')

我已经在表格中添加了此字段,以便该人选择他/她正在申请的工作。这样可以纠正错误或将您推向正确的方向。