LoginSignup
3
4

More than 5 years have passed since last update.

[Django] ChoiceFieldに初期値を与える

Posted at

環境
Python 3.5.0
Django 1.9.2

forms.py
CAKE_CHOICES = (
    (1, 'いちごケーキ'),
    (2, 'チョコケーキ'),
    (3, 'チーズケーキ')
)

class CakeForm(forms.Form):
    medal = ChoiceField(widget=Select, choices=MEDAL_CHOICES)

    class Meta:
        model = Cake
views.py
cake_form = CakeForm(initial = {
    'selected_cake': 2  # チョコケーキを初期値に設定
})
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4