On Sat, Mar 8, 2008 at 6:49 AM, speleolinux <[EMAIL PROTECTED]> wrote:
> > Hi > > On Mar 7, 11:26 pm, Malcolm Tredinnick <[EMAIL PROTECTED]> > wrote: > > It is fairly important when reporting an error like this that you give > > enough information to indicate where the error is coming from. > ... > Sorry. See at bottom. > > > 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. > > Correct, it just has one field 'choices'. I mentioned in the post that > the model has 'choices', the Form will have choice1, choice2 etc. > > > 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. > > Yes I realise that I have to do the work in setting a value for the > field 'choices' for the model > from the values choice1 & choice2 in the form which should be in > cleaned_data if the form is valid. > I thought something like this is what I would do: > > def clean(self): > # later I'll do a loop as there are a few of these choices. > self.cleaned_data['choices'] = self.cleaned_data['choice1'] + > self.cleaned_data['choice2'] > # The model does not contain choice1, choice2 etc so remove > them. > del self.base_fields['choice1'] > del self.base_fields['choice2'] > # Now the form data should match the model so it should 'save' > OK. > return self.cleaned_data > If you are going to populate a Decision from self.cleaned_data, then you need to remove choice1 and choice2, etc, from the cleaned_data dictionary that you return from your clean() function. Removing them from base_fields I don't think does much at this point, they're already in cleaned_data and that is where they need to be removed from. [ Also am I right in my thinking that the clean method of the custom > form in forms.py > is where I should do this rather than in say views.py as that way the > fact that the form uses > choice1, choice2 etc => choices is kept out of the views and out of > the model. ] > I'm not sure where the right place is for this, mucking with removing fields from cleaned_data seems inelegant but if you're going to be presenting a set of fields on the form and have to consolidate them down to one field for the model then it's got to be done somewhere. > I presume that as the form should be valid there will be a > cleaned_data dict available. > So I still can't understand why I get TypeError at /planner/decision/ > add/ > 'choice1' is an invalid keyword argument for this function > The cleaned_data dict that is available is the one returned from your clean() function. Since you haven't removed the clean1, clean2 choices from that, you get the TypeError. 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---