简体   繁体   English

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

[英]null value in column “job_id” violates not-null constraint

I'm trying to upload a pdf file but I keep getting an integrity error, when I try to submit the pdf file, it looks like I have some blank space in the DB which I don't know, may someone please help me! 我正在尝试上传pdf文件,但是我一直收到完整性错误,当我尝试提交pdf文件时,好像我在数据库中有一些空白,我不知道,也许有人请帮我! The error is: 错误是:

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

models.py 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 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 urls.py

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





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

the results is 结果是

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).

Your Application model has a ForeignKey to Jobs model (which I think should be just Job , but that's another matter), but it is not nullable/blank. 您的Application模型具有ForeignKey to Jobs模型(我认为应该只是Job ,但这是另一回事),但是它不能为空/空白。 It seems to me your view does not provide a Jobs instance to the Application instance that's being created with the PDF upload, hence the error. 在我看来,您的视图没有向使用PDF上传创建的Application实例提供Jobs实例,因此出现错误。

Update: 更新:

One way of doing this would be to add null=True, blank=True to your job field declaration on the Application model. 一种方法是在Application模型上的job字段声明中添加null=True, blank=True However, this doesn't sound like a good solution, as an application should be associated to a job. 但是,这听起来不是一个好的解决方案,因为应将应用程序与工作相关联。 So you need to allow the user to pick the job that he's applying to in the Application form or automatically assign that job to the application when you process the form submission in the CreateApplicationView . 因此,当您在CreateApplicationView处理表单提交时,您需要允许用户在“ Application表单中选择他要申请的工作,或自动将该工作分配给应用程序。

Update 2: 更新2:

In your ApplicationForm , try to do this: 在您的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')

I've added the field in the form for the person to pick the Job he/she's applying for. 我已经在表格中添加了该字段,以便该人选择他/她正在申请的工作。 This should fix the error or push you in the right direction. 这样可以解决错误或将您推向正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:关系的“id”列中的 null 值违反非空约束 - Django : null value in column "id" of relation violates not-null constraint “ user_id”列中的空值违反了非空约束 - null value in column “user_id” violates not-null constraint “ city_id”列中的空值违反了非空约束 - null value in column “city_id” violates not-null constraint Flask SQLAlchemy null “id”列中的值违反非空约束 - Flask SQLAlchemy null value in column "id" violates not-null constraint Django-“ imageLink”列中的空值违反了非空约束 - Django - Null value in column “imageLink” violates not-null constraint Django - 列中的空值违反了 Django 管理中的非空约束 - Django - null value in column violates not-null constraint in Django Admin 保存ModelForm时,列中的null值违反非空约束 - null value in column violates not-null constraint while saving ModelForm Django:“is_admin”列中的 null 值违反了非空约束 - Django: null value in column "is_admin" violates not-null constraint IntegrityError:“city_id”列中的空值违反非空约束 - IntegrityError: null value in column “city_id ” violates not-null constraint 关系“blog_comment”的“comment_id”列中的 null 值违反了非空约束 - null value in column “comment_id” of relation “blog_comment” violates not-null constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM