On Dec 17, 12:49 pm, marty3d <martin.kjellb...@gmail.com> wrote:
> Hi!
>
> I've been stuck with this for some days now, and can't find any
> answers whatsoever. Please bare with me since I'm a newbie with Python/
> Django.
>
> I have this form that's created and populated using
> 'modelformset_factory'. What it's supposed to do is to
> 1. show a number of sets so the user can add several at one time. Also
> show the added ones, with the possibility to delete them if necessary.
> 2. Save content. This means insert/update/delete.
>
> Easy enough, ey? But I'm stuck at the error "AttributeError:
> 'OptionForm' object has no attribute 'cleaned_data'". Strange things
> happen here. "OptionForm" doesn't exist for example. I guess Django
> adds "Form" by itself, why I don't know.
>
> The full traceback:
<snip>
> AttributeError: 'OptionForm' object has no attribute 'cleaned_data'
>
> Code:
> models.py:
> ---------
> class Option(models.Model):
>     option_name = models.CharField(max_length=200)
>     option_value = models.CharField(max_length=200)
>     default_selected = models.BooleanField(default=False)
>     def __unicode__(self):
>         return self.option_name
>
> views.py:
> --------
> from models import Option
>
> def OptionEdit(request, invite_id=None, form_id=None, field_id=None):
>     from django.forms.models import modelformset_factory
>     instance = Option.objects.all()
>     if request.method == 'POST':
>         OptionFormSet = modelformset_factory(Option)
>         formset = OptionFormSet(request.POST, request.FILES,
> queryset=instance)
>         formset.save()
>         return HttpResponseRedirect(reverse(OptionEdit, args=
> [invite_id, form_id, field_id]))
>     else:
>         OptionFormSet = modelformset_factory(Option, extra=2,
> can_delete=True)
>         formset = OptionFormSet(queryset=instance)
>         return render_to_response('invites/options_edit.html', {
>                                 'formset': formset,
>                                 'invite_id': invite_id,
>                                 'form_id': form_id,
>                                  })
>
> options_edit.html:
> -----------------
> <form name="frm1" method="post" action="">
>         <table>
>                 {{ formset }}
>                 <tr>
>                         <td colspan="2"><hr /></td>
>                 </tr>
>         </table>
>         <input type="submit" />
> </form>
>
> So, I'm really appreciate if you could help out an old VB coder with
> this mess. Otherwive it seems like a super framework, if you only
> could decode those error messages that's not very clear all the time.
>
> Thanks!
> /Martin

The problem is probably that you're not checking that the formset is
valid before saving it. If a form doesn't pass validation, it doesn't
have a cleaned_data attribute. So the relevant part of your view
should be something like:

    if request.method == 'POST':
        OptionFormSet = modelformset_factory(Option)
        formset = OptionFormSet(request.POST, request.FILES,
queryset=instance)
        if formset.is_valid():
            formset.save()
            return HttpResponseRedirect(reverse(OptionEdit, args=
[invite_id, form_id, field_id]))

Then bring the render_to_response line back one indent level, so that
an invalid form falls through and renders again - showing the errors.
---
DR.
--~--~---------~--~----~------------~-------~--~----~
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