我想更新多个对象(表单集)。我有一个模型,可以用这个创建多个对象:
class FormSeguridadView(CreateView):
v = 0
insp = 0
form_class = RespFormSeguridad
RespFormset = formset_factory(RespFormSeguridad, extra=0)
template_name = 'formularios/formSeguridad.html'
def get_context_data(self, **kwargs):
v = self.v
insp = self.insp
context = super(FormSeguridadView, self).get_context_data(**kwargs)
context['pregDocuObra'] = PRG.objects.filter(PRG_TIP=1)
context['seguridad_form'] = self.RespFormset(initial=[
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 11},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 12},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 13},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 14},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 19},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 20},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 21},
{'RESP_INSP': insp, 'RESP_FORM': v, 'RESP_PRG': 22},
])
return context
def post(self, request, *args, **kwargs):
resp_formset = self.RespFormset(self.request.POST, self.request.FILES)
if resp_formset.is_valid():
for respuestas in resp_formset:
respuestas.save()
return HttpResponseRedirect(reverse("index"))
else:
print(resp_formset.errors)
return HttpResponseRedirect(reverse("index"))
这是我来自form.py的部分:
class RespFormSeguridad(forms.ModelForm):
class Meta:
model = RESP
fields = '__all__'
def __init__(self,*args, **kwargs):
super(RespFormSeguridad, self).__init__(*args, **kwargs)
self.fields['RESP_FORM'].widget = forms.HiddenInput()
self.fields['RESP_PRG'].widget = forms.HiddenInput()
self.fields['RESP_INSP'].widget = forms.HiddenInput()
self.fields['RESP_RESUL'].widget.attrs['class'] = 'form-control'
但是我尝试使用以下代码进行更新:
class FormSeguridadUpdateView(UpdateView):
form_class = RespFormSeguridad
template_name = 'formularios/formSeguridadUpdate.html'
queryset = RESP.objects.all()
def get_object(self, queryset=None):
id_ = self.kwargs['pk']
return get_list_or_404(RESP, RESP_INSP=2)
def form_valid(self, form):
print(form.cleaned_data)
return super().form_valid(form)
但是它不起作用。引发错误:
Traceback:
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\edit.py" in get
190. return super().get(request, *args, **kwargs)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\edit.py" in get
133. return self.render_to_response(self.get_context_data())
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\edit.py" in get_context_data
66. kwargs['form'] = self.get_form()
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\views\generic\edit.py" in get_form
33. return form_class(**self.get_form_kwargs())
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\app\forms.py" in __init__
37. super(RespFormSeguridad, self).__init__(*args, **kwargs)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\forms\models.py" in __init__
292. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "C:\Users\usuario\Desktop\Proyectos\inspecciones-moneleg\venv\lib\site-packages\django\forms\models.py" in model_to_dict
82. opts = instance._meta
Exception Type: AttributeError at /inspections/1/updateFormSeguridad/
Exception Value: 'list' object has no attribute '_meta'
我不知道该如何解决。也许这不是编辑多个对象的解决方案。如果有人知道解决此问题,请在此处评论。
答案 0 :(得分:0)
The error is being raised because you return a list from the method get_object()
, but that method should return a model instance (an instance of a subclass of models.Model
).
def get_object(self, queryset=None):
...
return get_list_or_404(RESP, RESP_INSP=2) # this causes the ERROR
The linked related question mentioned in a comment to the question points this out as well.
I read your comment that you want to update many instances and not just one, but I don't think overriding get_object()
is the right way to do it.
That being said, I don't know how to solve your issue, I just added this answer to explain where the error is comming from.