Is this the correct way to update existing data using formset?

I have the following code to update my database using formset for one part of the form. I am able to get a queryset and display it currently. But when i submit back, di_form has the following error in my powershell []. Can anyone guide me on how to correct this? Thank you.

def device_edit(request, pk):
    device = Device.objects.get(pk=pk)
    deviceDetail = DeviceDetail.objects.get(DD2DKEY=pk)
    deviceInterface = DeviceInterface.objects.filter(I2DKEY = pk)
    
    if request.method == 'POST':
        dd_form = DeviceDetailForm(request.POST)
        di_formset = modelformset_factory(DeviceInterface, fields=('moduletype', 'firstportid', 'lastportid'), extra=1,max_num=3)
        di_form=di_formset(request.POST)
            if dd_form.is_valid():
                DeviceDetail.objects.filter(DD2DKEY=pk).update(mgt_interface=dd_form.cleaned_data['mgt_interface'],subnetmask = dd_form.cleaned_data['subnetmask'],ssh_id = dd_form.cleaned_data['ssh_id'],ssh_pwd = dd_form.cleaned_data['ssh_pwd'],enable_secret = dd_form.cleaned_data['enable_secret'], dev_mod = dd_form.cleaned_data['dev_mod'])
                if di_form.is_valid():
                    for instances in deviceInterface:
                        DeviceInterface.objects.filter(I2DKEY = pk).update(moduletype = di_form.cleaned_data['moduletype'], firstportid = di_form.cleaned_data['firstportid'], lastportid = di_form.cleaned_data['lastportid'])
        
                    return redirect('/device/', {'device':Device.objects.all, 'devices':device})
                else:
                    print(di_form.errors)
    else:
        dd_form = DeviceDetailForm()
        di_form = DeviceInterfaceForm()
    
    return render(request, 'interface/device_edit.html',{'dd_form': dd_form, 'di_form': di_form, 'devices':device, 'deviceDetail':deviceDetail, 'deviceInterface':deviceInterface })

Rather than trying to manage the relationships yourself between the DeviceInterface and DeviceDetail object, let Django do that work for you - see the InlineFormsets for the objects and code to handle a formset related to a model by a foreign key. Take particular note of the section Using an inline formset in a view.