我有一个称为工作,应用程序,附件的模型。当已经创建了一个工作,而一个用户想要申请该工作时,我收到一个完整性错误,提示null value in column "job_id" violates not-null constraint
如果我在class Meta
的{{1}}的字段中添加作业,则一切运行成功,但是当用户选择该作业时,他要应用的内容将被迫再次选择该作业以填写该作业领域,
所以我想要的是自动添加要在views.py
这是我的代码
models.py
ApplicationForm
form.py
class Application(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='applied_jobs',
null=True, on_delete=models.SET_NULL)
job = models.ForeignKey(Job, related_name='applications', on_delete=models.CASCADE)
career_briefing = models.CharField(max_length=250, null=True)
class Attachments(models.Model):
application = models.ForeignKey(Application, related_name='attachments', on_delete=models.CASCADE)
cv = models.CharField(max_length=150, null=True)
views.py
class ApplicationForm(forms.ModelForm):
cvs = forms.CharField(max_length=6000, widget=forms.HiddenInput(), required=False)
class Meta:
model = Application
fields = ('career_briefing',)
urls.py
class ApplicationCreateView(LoginRequiredMixin, CreateView):
model = Application
form_class = ApplicationForm
message = _("succesfuly applied")
template_name = 'jobs/application_form.html'
message = _("successful")
def form_valid(self, form):
cvs = form.cleaned_data['cvs']
form.instance.user = self.request.user
form.instance.job = self.kwargs.get('pk')
apply = form.save(commit=False)
apply.job = self.kwargs.get('pk')
apply.user = self.request.user
apply.save()
doc = Attachments(cv=cvs)
doc.application = apply
doc.save()
return super(ApplicationCreateView, self).form_valid(form)
def get_success_url(self):
messages.success(self.request, self.message)
return reverse("jobs:list")
我希望用户成功提交了申请表,但是我得到了urlpatterns = [
....
url(r'^details/(?P<pk>\d+)/$', JobDetailView.as_view(), name='detail'),
url(r'^details/apply/$', ApplicationCreateView.as_view(), name='apply'),
.....
]
对我来说,我认为integrity error " null value in column "job_id" violates not-null constraint ",
未保存,如果这是真正的问题,可能有人请教我如何解决它,
如果不是那样的话,有人可以帮我解决这个问题
预先感谢