For a formwizard, I'm trying to set the values of a
ModelMultipleChoiceField in the form's clean() method, based on the
cleaned_data from another field, and it's not working.

Here's roughly what I'm trying to do:

# forms.py
from django import forms
from myapp.models import MyModel

class MyForm(forms.Form):
    codes = forms.CharField(widget=forms.Textarea)
    items =
forms.ModelMultipleChoiceField(queryset=MyModel.objects.all(),
required=False)

    def clean_codes(self):
        codes = self.cleaned_data.get('codes')
        items = []
        for code in codes.split():
             try:
                 items.append(MyModel.objects.get(code=code)
             except MyModel.DoesNotExist:
                 raise forms.ValidationError('Invalid code: %s' %
code)
        self.items = items
        return codes

    def clean(self):
        super(MyForm, self).clean()
        data = self.cleaned_data
        data['items'] = self.items
        return data

I've also tried assigning a list of model IDs, a string representation
of model IDs, or a queryset of model objects to data['items'], but
regardless of what I do, the items field returns/displays no values
selected.  Any suggestions?

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