On Mar 9, 1:31 am, "Karen Tracey" <[EMAIL PROTECTED]> wrote:
> 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.

Ah thanks! For a start I was removing the fields from the wrong place.
I really needed to be doing this:
    del self.cleaned_data['choice1']

> 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.

Inelegant. Oh well :-(
But yes I have to consolidate several fields into one in the model.

I now have this:

def clean(self):
       enumerated_choices = []
       if self.cleaned_data['choice1']:
 
enumerated_choices.append( (str(self.cleaned_data['choice1']),
int(self.cleaned_data['decision1'])) )
            del self.cleaned_data['choice1']
            del self.cleaned_data['decision1']
        if self.cleaned_data['choice2']:
 
enumerated_choices.append( (str(self.cleaned_data['choice2']),
int(self.cleaned_data['decision2'])) )
            del self.cleaned_data['choice2']
            del self.cleaned_data['decision2']

        self.cleaned_data['choices'] = str(enumerated_choices)
        return self.cleaned_data

def save(self):
        d = Decision(**self.cleaned_data)
        d.save()
        return '%s' % d.id

and it works !
Now it works I have to better understand what's happening and to
iterate over the choices so that it can do any number of fields.
(Basically a user enters a description of a choice and a drop down and
selects a decision. There can be several of these.
The data is turned into a stringified list of tuples and stored in the
database in one string field.)

Thanks Karen for the clue that got it working.

Mike


--~--~---------~--~----~------------~-------~--~----~
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