On Fri, 2008-03-07 at 16:59 +1100, Michael Lake wrote:
[...]
> In forms.py I have:
> 
> class AddDecisionForm(forms.Form):
> 
>     choice1 = forms.CharField(label='choice1', required=False)
>     choice2 = forms.CharField(label='choice2', required=False)
> 
>     enumerated-choices = []
>     if choice1:
>         enumerated-choices.append(choice1)
>     if choice2:
>         enumerated-choices.append(choice2)
>     etc....
> 
>     # Include this here so the form has the field that can be saved to
> the model.
>     choices = forms.CharField(initial='1,2', required=False,
>         widget=forms.HiddenInput)
> 
>     def save(self):
>         d = Decision(**self.cleaned_data)
>         d.choices = str(enumerated_choices)
>         d.save()
> 
> The above basically outlines "what" I want to do but does not work.

[...]
> I'm getting an error when I click Add.
> "TypeError at /planner/decision/add/
> 'choice1' is an invalid keyword argument for this function.

It is fairly important when reporting an error like this that you give
enough information to indicate where the error is coming from. That is
why Python gives you a full traceback. You've done the equivalent of
reporting "somebody has been hit by a car" without giving the street
address.

Guessing a lot here (it looks like there's only one place "choice1"
could be being used as a keyword argument), I suspect you'll find the
problem is related to this line in your view:

        d = Decision(**self.cleaned_data)
        
Since your form has fields 'choice1' and 'choice2', this will be
equivalent to a call to

        Decision(choice1=..., choice2=..., choices=...)
        
and I'll wager that your Decision model doesn't have fields of that
name. Remember that the cleaned_data dictionary contains one key for
each field in your form. So if they don't map exactly onto the field
names for your model (with no extras -- it's not going to magically read
your mind and decide you didn't mean to pass in choice1 and choice2),
you'll need to manually map from the cleaned_data entries to the
keywords to pass to the Decision constructor.

Regards,
Malcolm


> 
> I have read over
> http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation
> and http://www.b-list.org/weblog/2007/nov/23/newforms/
> But still having problems working how how to clean this form data. I
> understand that I need to probably do some processing in a clean
> method to concatenate the choice1 etc values to choices but maybe even
> use del self.base_fields['choice1'] etc somehow to remove them from
> the form before it validates?
> I'm just really confused.
> 
> Mike
-- 
On the other hand, you have different fingers. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to