为了账号安全,请及时绑定邮箱和手机立即绑定

编辑 Django 表单集数据创建新记录而不是更新

编辑 Django 表单集数据创建新记录而不是更新

PHP
收到一只叮咚 2023-11-09 17:06:09
请帮忙!我正在尝试通过主表单 pk 更新一组与主表单(项目信息)相关的表单集。创建表单工作正常,但我发现实现更新版本极其困难。这是另存为更新部分,我无法弄清楚formsets。主窗体正确更新数据库。表单集只是创建新记录而不是更新现有记录。我不知道如何实例化表单集。这是代码。view.py@login_requireddef edit_project(request):    SampleInformationFormSet = formset_factory(SampleInformationForm, extra=1)    DissolutionMethodsFormSet = formset_factory(DissolutionMethodsForm, extra=1)       form = ProjectInformationForm(request.POST or None)    si_formset = SampleInformationFormSet(request.POST or None, prefix='si')    d_formset = DissolutionMethodsFormSet(request.POST or None, prefix='d')        if request.method == 'POST':        form = ProjectInformationForm(request.POST or None)        si_formset = SampleInformationFormSet(request.POST or None, prefix='si')        d_formset = DissolutionMethodsFormSet(request.POST or None, prefix='d')        pi_pk = ''        p = ''        if form.is_valid():            pi_pk = form.cleaned_data['hd']            p = ProjectInformation.objects.get(pk=pi_pk)            form = ProjectInformationForm(request.POST, instance=p)            form.save()  # This form saves correctly        for si_form in si_formset:            si_form.save() # I do not know how to attach an instance to these formsets        for d_form in d_formset:            d_form.save()                    messages.success(request, 'Your project has been updated!')            return redirect('edit_project')        else:            pass        form = ProjectInformationForm(request.POST or None)        si_formset = SampleInformationFormSet(request.POST or None, prefix='si')        messages.warning(request, 'There was an error saving your form.')    study_id_select = list(        ProjectInformation.objects.values('dissolution_study_id', 'id').filter())    context = {'form': form, 'si_formset': si_formset, 'd_formset': d_formset, 'study_id_select': study_id_select}    return render(request, 'main/edit_project.html', context)
查看完整描述

1 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

最后自己解决了。


@login_required

def edit_project(request):

    SampleInformationFormSet = formset_factory(SampleInformationForm, extra=1)

    DissolutionMethodsFormSet = formset_factory(DissolutionMethodsForm, extra=1)

   

    form = ProjectInformationForm(request.POST or None)

    si_formset = SampleInformationFormSet(request.POST or None, prefix='si')

    d_formset = DissolutionMethodsFormSet(request.POST or None, prefix='d')

    

    if request.method == 'POST':

        form = ProjectInformationForm(request.POST or None)

        si_formset = SampleInformationFormSet(request.POST or None, prefix='si')

        d_formset = DissolutionMethodsFormSet(request.POST or None, prefix='d')


        pi_pk = ''

        p = ''


        if form.is_valid():

            pi_pk = form.cleaned_data['hd']

            p = ProjectInformation.objects.get(pk=pi_pk)

            form = ProjectInformationForm(request.POST, instance=p)

            form.save()


        si = p.sampleinformation_set.all()  # All the Methods related to the instance

        d = p.dissolutionmethods_set.all()


        si_cnt = 0

        for si_form in si_formset:

            if si_form.is_valid():

               update = si_form.save(commit=False)

               update.pk = si[si_cnt].pk

               update.save()

               si_cnt += 1


        d_cnt = 0

        for d_form in d_formset:

            if d_form.is_valid():

                update = d_form.save(commit=False)

                update.pk = d[d_cnt].pk

                update.save()

                d_cnt += 1


查看完整回答
反对 回复 2023-11-09
  • 1 回答
  • 0 关注
  • 1494 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信