Within my ModelForm I have created a dropdown that isn’t bound to anything on the model directly. Hence I pass in the queryset for it upon instantiation.
class CallsForm(ModelForm):
def __init__(self, company, *args, **kwargs):
super(CallsForm, self).__init__(*args, **kwargs)
self.fields['test_1'].queryset = company.deal_set.all()
test_1 = forms.ModelChoiceField(queryset = '')
This works just fine. However how do I specify some attributes for it?
For the other model-bound-widgets I usually do this in Meta:
class Meta:
model = Conversation
widgets = {
'notes': forms.Textarea(attrs={'class': 'red'}),
}
But overriding it in my case would make no sense.
I tried to set the attributes upon instantiation without any luck.
test_1 = forms.ModelChoiceField(attrs={'class':'hidden'}, queryset = '')
but it says: __init__() got an unexpected keyword argument 'attrs'
Surely there must be a way…
attrsis only valid on widgets, not fields. Try: