我有一个网页包含一个工作请求和inlinformset为可能的候选人设置一个按钮来设置面试工作正常。对于每个候选人,对于相同的工作请求,应该有一次或多次面试。当用户点击面试按钮时,他将被转移到另一个页面,用于采访URL Para作为工作申请ID(jid)和候选ID(cid),如下所示
class InterviewForm(ModelForm):
class Meta:
model = Interview
exclude = ()
InterviewFormSet = modelformset_factory(Interview,form=InterviewForm, extra=1)
面试模型
class Interview(models.Model):
jobRequisition = models.ForeignKey(JobRequisition, on_delete=models.PROTECT)
interviewer = models.ManyToManyField(Employee)
candidate = models.ForeignKey(Candidate, on_delete=models.PROTECT)
interview_date = models.DateField( blank = True)
interview_type = models.CharField(max_length=150, choices= interview_type )
视图:
class InterviewCreate(CreateView):
model = Interview
form_class = InterviewForm
success_url = reverse_lazy('jobRequisitions-list')
def get_context_data(self, **kwargs):
# bring all interviews related to the selected candidate and job request
qu = Interview.objects.filter(jobRequisition=self.kwargs['jid'], candidate=self.kwargs['cid'])
context = super(InterviewCreate, self).get_context_data(**kwargs)
if self.request.POST:
context['candidateinterviews'] = InterviewFormSet(self.request.POST)
else:
context['candidateinterviews'] = InterviewFormSet(queryset=qu)
return context
我的问题: 1-无法保存:当我点击保存按钮时,我收到一些错误来填写缺失的数据,因此,验证工作正常,但在此之后,我无法将数据保存在表单集中。我曾尝试使用" def form_valid(self,form):print("我在这里")"但似乎代码没有进入这个功能,它总是得到form_invalid。
2- url para中的默认值:我想隐藏用户的候选人姓名和工作申请号,当他点击“保存”时,这些数据将从网址中自动提取并与其他条目一起保存。我可以从url获取para,但是我无法将它们附加到formst并将它们保存到数据库中。
更新后的视图:
def InterviewCreate(request, jid, cid):
jobRequisitions_url = '/recruitment_single_company/jobRequisitions/' + str(jid)
if request.method == "POST":
candidateinterviews = InterviewFormSet(request.POST, request.FILES)
if(candidateinterviews.is_valid()):
candidateinterviews.save()
return HttpResponseRedirect(jobRequisitions_url)
else:
print ("Something went wrong" )
else:
candidateinterviews = InterviewFormSet(queryset=Interview.objects.filter(jobRequisition=jid, candidate=cid))
return render(request, 'recruitment_single_company/interview_form.html', {'formset': candidateinterviews})
到目前为止,只要我手动输入job_id和candidate_id作业,代码就会消失,但是我希望隐藏这两个文件并在表单有效时保存它们。
我已经尝试在上面的代码中替换前一行并添加了初始数据(如下所示)并且它工作正常,但是当我想使用&#34时排除两个fieldes;排除"我无法保存表单。我可以隐藏那些不包括它们的字段吗?
candidateinterviews = InterviewFormSet(queryset=Interview.objects.filter(jobRequisition=jid, candidate=cid), initial=[{'jobRequisition': jid, 'candidate': cid,}])
答案 0 :(得分:1)
以下是我的最终观点,我正在使用jquery.formset.js
def InterviewCreate(request, jid, cid):
jobRequisitions_url = '/recruitment_single_company/jobRequisitions/' + str(jid)
if "cancel_btn" in request.POST: # if user click cancel/back button then it will go to previous page
return HttpResponseRedirect(jobRequisitions_url)
if request.method == "POST":
candidateinterviews = InterviewFormSet(request.POST, request.FILES)
if candidateinterviews.is_valid() and candidateinterviews.has_changed():
for form in candidateinterviews:
interview_date = form.cleaned_data.get('interview_date')
if form.cleaned_data.get('DELETE') and form.cleaned_data.get('id') != None:
interview_id = form.cleaned_data.get('id').id
Interview.objects.filter(id=interview_id).delete()
# Belw is my way to do extra validation, without it Django will try to save data
# even if there is no change and it will give the below error:
# The Interview could not be created because the data didn't validate.
elif interview_date != None:
obj = form.save(commit=False) # we need this because we have excluded fields in the form
obj.jobRequisition_id = jid # manually save the foreign key for job requestion
obj.candidate_id = cid # manually save the foreign key for candidate
obj.save()
form.save_m2m() # we need this since we have many to many fields
else:
candidateinterviews = InterviewFormSet(queryset=Interview.objects.filter(jobRequisition=jid, candidate=cid))
else:
candidateinterviews = InterviewFormSet(queryset=Interview.objects.filter(jobRequisition=jid, candidate=cid))
return render(request, 'recruitment_single_company/interview_form.html', {'candidateinterviews': candidateinterviews})
另外,我更新了InterviewForm以隐藏两列
class InterviewForm(ModelForm):
class Meta:
model = Interview
exclude = (['jobRequisition', 'candidate'] )
InterviewFormSet = modelformset_factory(Interview, form=InterviewForm, extra=1, can_delete=True)