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

请帮忙!

我正在尝试通过主表单 pk 更新一组与主表单(项目信息)相关的表单集。创建表单工作正常,但我发现实现更新版本极其困难。

这是另存为更新部分,我无法弄清楚formsets。主窗体正确更新数据库。表单只是创建新记录而不是更新现有记录。我不知道如何实例化表单集。

这是代码。

view.py

@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()  # 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)


收到一只叮咚
浏览 1497回答 1
1回答

侃侃无极

最后自己解决了。@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()        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
打开App,查看更多内容
随时随地看视频慕课网APP