これは私がはいラジオボタンなどの形でブーリアンモデルフィールドを表示するには、歩き回った方法で、第ラジオボタンとしてDjangoのフォームで表示ブールモデルフィールドではなく、デフォルトのチェックボックス
choices = ((1,'Yes'),
(0,'No'),
)
class EmailEditForm(forms.ModelForm):
#Display radio buttons instead of checkboxes
to_send_form = forms.ChoiceField(choices=choices,widget=forms.RadioSelect)
class Meta:
model = EmailParticipant
fields = ('to_send_email','to_send_form')
def clean(self):
"""
A workaround as the cleaned_data seems to contain u'1' and u'0'. There may be a better way.
"""
self.cleaned_data['to_send_form'] = int(self.cleaned_data['to_send_form'])
return self.cleaned_data
上記のコードでわかるように、入力文字列を整数に変換するクリーンなメソッドが必要です。これは不要かもしれません。
これを行うには、より良い方法やジャンゴーイックな方法がありますか?もしそうなら、どうですか?
いいえ、BooleanField
を使用すると、さらに多くの問題が発生するようです。それを使って私には明らかでした。しかし、そうではありません。なぜそうですか?
選択肢がペアのシーケンスであることに注意してください(http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ChoiceFieldを参照してください)。ペアに何が入っているのか分かりません。 – dfrankow
widgets.pyを見ると、choicesはフォームのタプルのリスト(choice_value、choice_label)です。 – dfrankow