关于python:内联formset is_valid错误:列表索引超出范围

inline formset is_valid error: list index out of range

尊敬的Django专家:

将内联表单集与基于类的视图合并,通过列表索引的异常无效,超出范围。

我想我的代码相当严格。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#view.py
class EntityUpdate(updateView):
    def post(self, request, *args, **kwargs):
        self.object=None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        form =myforms.add_place(request.POST)
        print(form.instance)
        urls_form = myforms.url_formset(self.request.POST , instance = form.instance)
        if form.is_valid() and urls_form.is_valid():
            return self.form_valid(form , urls_form)
        else:
            return self.form_invalid(form, urls_form)

#forms.py
#inlineformset definition
url_formset = inlineformset_factory(parent_model=Place,model=Urls , extra=1 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Traceback:
File"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File"/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File"/usr/local/lib/python2.7/dist-packages/braces/views.py" in dispatch
  140.             request, *args, **kwargs)
File"/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File"/home/ramez/code/eventsrun-project/eventsrun/eventsrunapp/views.py" in post
  483.         if form.is_valid() and urls_form.is_valid():
File"/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in is_valid
  292.         err = self.errors
File"/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in errors
  267.             self.full_clean()
File"/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in full_clean
  314.             form = self.forms[i]
File"/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
  49.         res = instance.__dict__[self.func.__name__] = self.func(instance)
File"/usr/local/lib/python2.7/dist-packages/django/forms/formsets.py" in forms
  133.         forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
File"/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
  848.         form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
File"/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _construct_form
  572.             kwargs['instance'] = self.get_queryset()[i]
File"/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __getitem__
  115.             return self._result_cache[k]

Exception Type: IndexError at /places/Place-formset/edit/
Exception Value: list index out of range

知道这是什么吗?

提前谢谢

编辑:我还尝试跳过验证(is_valid()),但在.save()中仍然出现相同的错误。


结果我向inlineformset发送了一个空实例…不是很聪明,但出于某种原因,我认为表单(request.post)实际上会知道这是一个现有的记录。

工作实施:

1
2
3
4
5
6
7
8
9
10
class EntityUpdate(updateView):
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        p = get_object_or_404(Entity, slug=kwargs['slug'])
        urls_form = myforms.url_formset(self.request.POST , instance = p) #Loading the formset with the appropriate instance parameter
        if form.is_valid() and urls_form.is_valid():
            return self.form_valid(form , urls_form)
        else:
            return self.form_invalid(form, urls_form)