亲爱的Django专家;
将内联formset与基于类的视图合并,is_valid通过列表索引的异常超出范围
我的代码非常直率(我猜)
#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 )
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()中引发了同样的错误
答案 0 :(得分:1)
原来我发送了一个空实例到inlineformset ...不是很聪明但是出于某种原因我觉得那个表单(request.POST)实际上知道这是一个现有的记录。
工作实施:
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)