Thanks for the English lesson! :)

As you both pointed out, the .is_valid() should be used here. I
skipped that part because the data was indeed valid and still got the
error message. It has been tried, sorry for not mentioning that.
But actually, by trying it again, with the Werkzeug debugger turned
off (which generated the traceback in the first place) AND
using .is_valid(), it worked! (and now, with Werkzeug turned back on
as well)

I guess I've been at it so long my head has taken an extra spin :)

Thanks again and sorry for bothering you with my own *bleep*-ups!
/Martin



On Dec 17, 4:23 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:
> On Wed, Dec 17, 2008 at 7:49 AM, 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.
>
> (English nitpick: the word is bear, not bare, that you're looking for in
> that idiom.)
>
>
>
> > 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.
>
> You've left out a step here.  Before you can save content you need to verify
> that the data submitted by the user is valid.  You seem to have jumped into
> an advanced usage of forms without covering the basics:
>
> http://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-...
>
> Calling form.is_valid() and verifying that it returns True is essential
> before attempting to do anything (other than re-displaying it with the error
> annotations) with the form created from POST data.  You cannot save a form
> that is not valid.
>
>
>
> > 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.
>
> cleaned_data only exists for valid forms.  Any error like "whateverform has
> no attribute cleaned_data" means your code is proceeding under the
> assumption that the form has validated when in fact it has not.
>
> As for OptionForm, your code requests creation of a model formset from your
> Option model.  To do this, Django dynamically creates a ModelForm class and
> names it <your-model-name>Form.  Each individual form in your formset is an
> OptionForm.
>
>
>
>
>
> > The full traceback:
> > Traceback (most recent call last):
> >  File "C:\Program Files\Python25\Lib\site-packages\django\core\servers
> > \basehttp.py", line 635, in __call__
> >    return self.application(environ, start_response)
> >  File "C:\Program Files\Python25\Lib\site-packages\django\core
> > \handlers\wsgi.py", line 239, in __call__
> >    response = self.get_response(request)
> >  File "C:\Program Files\Python25\Lib\site-packages\django\core
> > \handlers\base.py", line 128, in get_response
> >    return self.handle_uncaught_exception(request, resolver, exc_info)
> >  File "C:\Program Files\Python25\Lib\site-packages\django\core
> > \handlers\base.py", line 148, in handle_uncaught_exception
> >    return debug.technical_500_response(request, *exc_info)
> >  File "C:\Program Files\Python25\Lib\site-packages\django\core
> > \handlers\base.py", line 86, in get_response
> >    response = callback(request, *callback_args, **callback_kwargs)
> >  File "C:\Projects\dis_dk\invites_project\invites\views.py", line
> > 242, in OptionEdit
> >    formset.save()
> >  File "C:\Program Files\Python25\Lib\site-packages\django\forms
> > \models.py", line 389, in save
> >    return self.save_existing_objects(commit) + self.save_new_objects
> > (commit)
> >  File "C:\Program Files\Python25\Lib\site-packages\django\forms
> > \models.py", line 403, in save_existing_objects
> >    obj = existing_objects[form.cleaned_data[self._pk_field.name]]
> > AttributeError: 'OptionForm' object has no attribute 'cleaned_data'
>
> This traceback confuses me.  It shows that in attempting to get the response
> [get_response(request)], an uncaught exception was raised
> [handle_uncaught_exception(request, ...), so it proceeds to begin to return
> a debug page [return debug.technical_500_response(request, ...)] but then we
> jump into code that is not part of the default debug.technical_500_response
> and that code looks like it tries calling your view code again, generating
> (probably) the same exception as what caused the call to
> "hanlde_uncaught_exception" in the first place.  Have you done something to
> modify the default technical_500_response?
>
>
>
>
>
> > 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()
>
> Here you are calling formset.save() without verifying formset.is_valid() is
> True.  That's what's leading to the exception.  But I find the code path
> shown in the traceback confusing and troublesome as well, and I can't
> explain that one uless you've done something to attempt to customize how
> Django handles 500 server errors.
>
> Karen
--~--~---------~--~----~------------~-------~--~----~
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