First of all, don't modify fields in __init__
, if you want to override widgets use Meta
inner class, if you want to override form fields, declare them like in a normal (non-model) form.
If the Select
widget does not do what you want, then simply make your own. Original widget uses render_option
method to get HTML representation for a single option — make a subclass, override it, and add whatever you want.
class MySelect(forms.Select):
def render_option(self, selected_choices, option_value, option_label):
# look at the original for something to start with
return u'<option whatever>...</option>'
class LocModelForm(forms.ModelForm):
icons = forms.ModelChoiceField(
queryset = Photo.objects.filter(galleries__title_slug = "markers"),
widget = MySelect(attrs = {'id': 'mydds'})
)
class Meta:
# ...
# note that if you override the entire field, you don't have to override
# the widget here
class Media:
# ...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…