谢谢你们的回复。虽然我将代码更改为以下内容,但我仍然得到相同的异常:
class ProjectUpdateView(UpdateView):
form_class = ProjectForm
template_name = 'projects/project_create.html'
success_url = reverse_lazy('projects-list')
def get_object(self, queryset=None):
obj = Project.objects.get(id=self.kwargs['pk'])
return obj
def get_context_data(self, **kwargs):
context = super(ProjectUpdateView, self).get_context_data(**kwargs)
project = Project.objects.get(id=self.kwargs['pk'])
if self.request.POST:
context['reward_formset'] = ProjectRewardFormSet(self.request.POST, self.request.FILES, instance=project)
else:
context['reward_formset'] = ProjectRewardFormSet(instance=project)
return context
def form_valid(self, form):
context = self.get_context_data()
reward_formset = context['reward_formset']
if reward_formset.is_valid():
self.object = form.save(commit=False)
self.object.owner = self.request.user
self.object.save()
reward_formset.instance = self.object
reward_formset.save()
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(self.get_context_data(form=form))
以下是追溯:
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view
20. return view_func(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/base.py in view
48. return self.dispatch(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/base.py in dispatch
69. return handler(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/edit.py in post
172. return super(BaseCreateView, self).post(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/edit.py in post
138. return self.form_valid(form)
...
▶ Local vars
/home/lara/.venv/reztarter/reztarter/../reztarter/projects/views.py in form_valid
22. context = self.get_context_data()
...
▶ Local vars
/home/lara/.venv/reztarter/reztarter/../reztarter/projects/views.py in get_context_data
39. context['reward_formset'] = ProjectRewardFormSet(self.request.POST)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in __init__
697. queryset=qs, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in __init__
424. super(BaseModelFormSet, self).__init__(**defaults)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/formsets.py in __init__
50. self._construct_forms()
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/formsets.py in _construct_forms
115. self.forms.append(self._construct_form(i))
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in _construct_form
706. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in _construct_form
451. kwargs['instance'] = self.get_queryset()[i]
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/db/models/query.py in __getitem__
190. return self._result_cache[k]
...
▶ Local vars
答案 0 :(得分:0)
您使用基于类的视图很奇怪。我不认为将此逻辑放在get_context_data方法中是可以的。你应该只创建formset和最重要的东西 - get_context_data应该返回上下文。 formset的验证应该在form_valid方法中进行。
答案 1 :(得分:0)
当有多个同步修改https://code.djangoproject.com/ticket/15574时,这是InlineFormSet的一个已知问题。
似乎内联formset代码有点脆弱。当管理表单中提交的数据与数据库状态不一致时,将引发错误。