ウィジェットを使用して、フォームのメタ定義のフィールドにカスタム属性を追加できます。
class SomeForm(forms.ModelForm):
class Meta:
model = SomeModel
widgets = {'field_name1': forms.Textarea(attrs={'data-bind':'value: field1'}),
'field_name2': forms.TextInput(attrs={'data-bind':'value: field2'})}
たとえば、最初のフィールドは次のようにレンダリングされます。
<textarea id="id_field_name1" name="field_name1" data-bind="value: field1"></textarea>
更新:
おまけとして、フォーム内のすべてのフィールドの属性を変更する簡単な方法があります。たとえば、特定のクラスがすべて必要な場合です (他の js アドオンや css スタイリングに役立ちます)。
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs['class'] = 'some_form_field'
# this could be used in your case if the Django field name is the
# same as the KO.js field name
field.widget.attrs['data-bind'] = 'value: %s' % name