My Mistake.

If anyone else is interested, I have been handling all my code for
this app through jquery and ajax calls. I didn't realise that each
form in the formset had a hidden field with its id. As I was creating
a new object for these formsets to relate to, all ids in the formset
needed to be set to u'' in order for new ones to be created that were
associated with the new object...if that makes sense!

Thanks anyway :)

On Sep 9, 2:53 pm, chewynougat <peter_i_campb...@hotmail.co.uk> wrote:
> Hi,
>
> I have instances in my code where I need to save an object with a new
> pk. Then I need to create and save an inline formset using the newly
> created object as the instance.
>
> form_instance = form.save(commit=False)
> form_instance.pk = None
> form_instance.slug = None
> form_instance.save()
>
> CardioFormset = inlineformset_factory(activitycentre_models.Activity,
> activitycentre_models.CardiovascularTraining)
>
> cardio_form = CardioFormset(request.POST, instance=form_instance) #
> This is the line that raises the IndexError exception.
>
> Could anyone shed any light as to where I am going wrong here? It is
> fine if I just save the form normally first (update).
>
> Much appreciated for any help on this issue!
>
> Traceback (most recent call last):
>   File "/Library/Python/2.5/site-packages/code/contrib/activitycentre/
> views.py", line 1711, in create_edit_event
>     cardio_form = InitialCardioFormset(request.POST, instance=a)
>   File "/Library/Python/2.5/site-packages/django/forms/models.py",
> line 446, in __init__
>     super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix
> or self.rel_name)
>   File "/Library/Python/2.5/site-packages/django/forms/models.py",
> line 335, in __init__
>     super(BaseModelFormSet, self).__init__(**defaults)
>   File "/Library/Python/2.5/site-packages/django/forms/formsets.py",
> line 67, in __init__
>     self._construct_forms()
>   File "/Library/Python/2.5/site-packages/django/forms/models.py",
> line 452, in _construct_forms
>     super(BaseInlineFormSet, self)._construct_forms()
>   File "/Library/Python/2.5/site-packages/django/forms/formsets.py",
> line 76, in _construct_forms
>     self.forms.append(self._construct_form(i))
>   File "/Library/Python/2.5/site-packages/django/forms/models.py",
> line 455, in _construct_form
>     form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
>   File "/Library/Python/2.5/site-packages/django/forms/models.py",
> line 339, in _construct_form
>     kwargs['instance'] = self.get_queryset()[i]
>   File "/Library/Python/2.5/site-packages/django/db/models/query.py",
> line 232, in __getitem__
>     return list(qs)[0]
> IndexError: list index out of range
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to