views:

34

answers:

1

Is there a more efficient, or cleaner way to do the following?

class SpamForm(ModelForm):
    some_choices = fields.MultipleChoiceField()

    def __init__(self, *args, **kwargs):
        super(SpamForm, self).__init__(*args, **kwargs)
        self.fields['some_choices'].choices = [[choice.pk, choice.description] for choice in self.instance.user.somechoice_set.all()]

    class Meta:
        model = Spam

This is to populate the new form with choices that pertain to the current request.user (which is set on the instance that's passed into the form). Is there a better way?

A: 

Use a ModelMultipleChoiceField in the form and set its queryset in __init__().

Ignacio Vazquez-Abrams
@Ignacio - the problem is that `ModelChoiceField` doesn't use a `multiple=multiple` select element, right?
orokusaki
My bad, I misread the question slightly. Answer updated.
Ignacio Vazquez-Abrams