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'
上記のコードでは、すべてのカテゴリ オブジェクトが一覧表示された選択ボックスが表示されます。私がやろうとしているのは、追加することです
<optgroup>VALUE</optgroup>
HTML 要素から特定の Category-Object (Category.parent == null のもの) へ。
誰もそれを行う方法を知っていますか? どうもありがとう!
PS: 私はすでに QuerySet を Choices-Set に変換しようとしました (例: http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/ )。 HTML をレンダリングするため - 不一致が発生する DB に結果を保存しようとするまで (ValueError)。