class CreateCourseForm(ModelForm):
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
empty_label="",
#widget=CustomCourseWidget()
)
class Meta:
model = Course
fields = ('title', 'description', 'category')
def __init__(self, *args, **kwargs):
super(CreateCourseForm, self).__init__(*args, **kwargs)
self.fields['category'].widget.attrs['class'] = 'chzn-select'
self.fields['category'].widget.attrs['data-placeholder'] = u'Please select one'
With the code above, I'm getting a select-box with all the Category-Objects listed. What I'm trying to do is to add a
<optgroup>VALUE</optgroup>
HTML-element to specific Category-Objects (the ones with Category.parent == null).
Does anyone know how to do that? Thanks a lot!
PS: I already tried converting the QuerySet to a Choices-Set (e.g. http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/), which works fine for rendering the HTML - until I try to save the results into the DB, where a mismatching occurs (ValueError).