1

谢谢你们的回复。尽管我将代码更改为以下内容,但我仍然遇到相同的异常:

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
4

2 回答 2

0

当有多个同时编辑https://code.djangoproject.com/ticket/15574时,这是 InlineFormSets 的一个已知问题。

似乎内联表单集代码有点脆弱。当管理表单中提交的数据与数据库的状态不一致时,会抛出错误。

于 2012-09-19T15:15:43.170 回答
0

您正在使用基于类的视图很奇怪。我认为将这个逻辑放在 get_context_data 方法中是不行的。你应该只把创建表单集和最重要的东西——get_context_data 应该返回上下文。表单集的验证应该在 form_valid 方法中进行。

于 2012-09-19T15:08:14.897 回答