I wanted to order a list in my form view, and found this post here:
How do I specify an order of values in drop-down list in a Django ModelForm?
So I edited my code and added the line specialty = forms.ModelChoiceField(queryset ='...')
So then I reload the form and the widget is all smoshed and funky looking. I checked the html code and the specialties are indeed in the right order! But it misses the widget definition lower adding the form-control class. I am not sure why. if I remove the line specialty = form.ModelChoiceField then everything looks great aside from the dropdown not being in the right order (alphabetical by name field)
Not sure why it is missing that widget definition and attaching the class form-control or the tabindex even. Guessing the specialty = forms.ModelChoiceField is overriding it somehow?
class ProviderForm(forms.ModelForm):
specialty = forms.ModelChoiceField(queryset = Specialty.objects.order_by('name')) #added this
def __init__(self, *args, **kwargs):
super(ProviderForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-6'
self.helper.field_class = 'col-md-6'
self.fields['ptan'].required = False
self.helper.layout = Layout(
Field('...'),
Field('specialty'),
FormActions(
Submit('save', 'Save'),
Button('cancel', 'Cancel'),
Button('terminate', 'Terminate', css_class="btn-danger"),
),
)
class Meta:
model=Provider
fields = (
'...',
'specialty'
)
widgets = {
'specialty':Select(attrs={
'tabindex':'4',
'class':'form-control'
}),
}