1
votes

Dear Django experts ;

incorporating inline formset with class based view , the is_valid through an exception of list index is out of range

My code is pretty straignt forward (i guess)

#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


Any Idea what is this ?

Thanks in advance

Edit: I also tried to jump over the validation (is_valid()), but stil same error got raised in the .save()

1
Inline formsets are not straightofrwardly supported in CBVs. Maybe this can help: stackoverflow.com/questions/4497684/…Berislav Lopac

1 Answers

1
votes

Well it turned out that i'm sending an Empty instance to inlineformset ... not very smart but for some reason i thought that form(request.POST) would actually know that this is an existing record.

Working implementation :

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)