gpt4 book ai didi

python - Django 动态表单集 UpdateView 不更新

转载 作者:行者123 更新时间:2023-12-01 06:37:00 27 4
gpt4 key购买 nike

我用了this使用 Django 成功设置动态内联表单集的教程。 CreateView 工作得很好,但我无法让 UpdateView 实际更新相关字段。没有抛出任何错误,但项目不会更新。我相信我已将错误隔离到 form_valid 函数。代码如下。感谢您的帮助。

class ApplicantCreate(CreateView):
model = Applicant
success_message = 'Your application was submitted successfully.'
form_class = forms.ApplicantForm
template_name = 'careers/add_applicant.html'
success_url = reverse_lazy('careers:thanks')

def get_context_data(self, **kwargs):
data = super(ApplicantCreate, self).get_context_data(**kwargs)
positions = super(ApplicantCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions

def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantCreate, self).form_valid(form)

def get_success_url(self):
return reverse_lazy('careers:thanks')

class ApplicantUpdate(SuccessMessageMixin,LoginRequiredMixin,GroupRequiredMixin,UpdateView):
group_required = [u'careers-admin',u'careers']
model = Applicant
success_message = '%(first_name)s %(last_name)s was updated successfully.'
form_class = forms.ApplicantUpdateForm
template_name = 'careers/edit_applicant.html'

def get_context_data(self, **kwargs):
data = super(ApplicantUpdate, self).get_context_data(**kwargs)
positions = super(ApplicantUpdate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
instance=self.object,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
instance=self.object,
prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions

def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantUpdate, self).form_valid(form)

def get_success_url(self):
return reverse_lazy('careers:applicant_detail',kwargs={'pk': self.object.pk})

最佳答案

这不是一篇好的博客文章,因为如果任何内联表单无效,则不会显示任何错误。作为用户,我不希望 View 只是默默地忽略内联表单中的错误,成功保存我的主实例并且不报告这些错误。

请注意,我不知道错误是什么,也许这只是管理表单的问题。但无论如何,应该在实际保存主对象之前处理表单集错误。

一般来说,如果需要编写具有多种表单(包括表单集)的 View ,最好使用基于函数的 View 或 View在你写get的地方和post而不是试图将其强制转换为通用的基于类的 View ,这并不是为此目的。

def multiple_forms_view(request, object_id):
# if this is a POST request we need to process the form data
obj = get_object_or_404(MyModel, pk=object_id)
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = MyForm(request.POST, instance=obj)
formset = MyFormSet(request.POST, instance=obj, ...)
# check whether it's valid:
if form.is_valid() and formset.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')

# if a GET (or any other method) we'll create a blank form
else:
form = MyForm(instance=obj)
formset = MyFormSet(instance=obj, ...)

return render(request, 'name.html', {'form': form, 'formset': formset})

这样您的模板就可以呈现每个表单的错误,包括表单集中的错误。如前所述,您也可以在 View 中执行此操作(这样你的 mixins 就可以工作),只需写 getpost方法和返回值与上面基于函数的 View 中的相同。

关于python - Django 动态表单集 UpdateView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com