3

I am trying to update the queryset of a ModelMultipleChoiceField in a ModelForm.

BaseWriteForm being a ModelForm, my class looks like as follows:

class MyWriteForm(BaseWriteForm):
    """The form for an authenticated user, to compose a message."""

    recipients = forms.ModelMultipleChoiceField(label=_('To'), 
                                                widget=forms.SelectMultiple(attrs={'class': 'chzn-select'}),
                                                queryset = User.objects.all())


    def __init__(self, users_list, **kw):
        self.fields['recipients'].queryset = User.objects.filter(pk__in=users_list)

        super(BaseWriteForm, self).__init__(**kw)


    class Meta(BaseWriteForm.Meta):
        fields = ('recipients', 'subject', 'body')

In this case I am having: 'MyWriteForm' object has no attribute 'fields'

Thanks !

ScotchAndSoda
  • 3,811
  • 4
  • 34
  • 38

1 Answers1

9

Just move it after the super() call().

def __init__(self, users_list, **kw):
    super(BaseWriteForm, self).__init__(**kw)
    self.fields['recipients'].queryset = User.objects.filter(pk__in=users_list)
AdamKG
  • 13,678
  • 3
  • 38
  • 46