I'm touching Choice Field in Django and I'll keep a reminder of the time-consuming parts. The sample code is here
-[About ChoiceField](About #ChoiceField) -Sample -[Parts that took time](#Parts that took time) -[How to add choices later](#How to add choices later) -[Field's choices` property](#field's choices property) -[ChoiceField code to add choices later](#Choicefield code to add choices later)
--A field to choose one from multiple choices
--The default drawing is django.forms.widgets.Select
--When drawn on the template, it becomes <select name =" field name "> <option value =" choice.key "> choice.value </ option> ... </ select>
--Reference: Form Fields: ChoiceField
from django import forms
class SampleChoiceForm(forms.Form):
choice1 = forms.fields.ChoiceField(
choices = (
('ja','Japan'),
('us','America'),
('uk','UK'),
('ch','China'),
('kr','Korea')
),
required=True,
widget=forms.widgets.Select
)
from django.shortcuts import render
from django.views import View
from . import forms
class SampleChoiceView(View):
def get(self, request):
form = forms.SampleChoiceForm()
context = {
'form': form
}
return render(request, 'choice_sample.html', context)
sample_choice_view = SampleChoiceView.as_view()
<!DOCTYPE html>
<html lang="ja">
<head>
<title> ChoiceField sample </ title>
</head>
<body>
<form method="POST" aciton="">
{% for field in form %}
{{ field }}
{% endfor %}
{% csrf_token %}
</form>
</body>
</html>
<!doctype html>
<html lang="ja">
<head>
<title> ChoiceField sample </ title>
</head>
<body>
<form method="POST" action="">
<select id="id_choice1" name="choice1">
<option value = "ja"> Japan </ option>
<option value = "us"> America </ option>
<option value = "uk"> UK </ option>
<option value = "ch"> China </ option>
<option value = "kr"> Korea </ option>
</select>
<input type="hidden" name="csrfmiddlewaretoken" value="xxxxxxxx" />
</form>
</body>
</html>
--How to add options later
In the official and Django Girls tutorials, fields with choices are written in the form of getting choices from the model or giving fixed choices as initial values in the ChoiceField's choice
parameter.
But what if you want to create this option dynamically?
choices
propertyThe Django documentation has a page called Model Field Reference (https://docs.djangoproject.com/en/2.2/ref/models/fields/)
It describes the fields used in the form, including model fields.
If you look for it, you will find the section Field.choices
.
Just in case, take a look at the ChoiceField source code.
class ChoiceField(Field):
(Omitted)
def _get_choices(self):
return self._choices
def _set_choices(self, value):
# Setting choices also sets the choices on the widget.
# choices can be any iterable, but we call list() on it because
# it will be consumed more than once.
if callable(value):
value = CallableChoiceIterator(value)
else:
value = list(value)
self._choices = self.widget.choices = value
choices = property(_get_choices, _set_choices)
(Omitted)
Certainly there is a property called choices
.
Let's use it.
from django import forms
(Omitted)
class SampleChoiceAddForm(forms.Form):
choice1 = forms.fields.ChoiceField(
required=True,
widget=forms.widgets.Select
)
from django.shortcuts import render
from django.views import View
from . import forms
(Omitted)
class SampleChoiceAddView(View):
def get(self, request):
form = forms.SampleChoiceAddForm()
form.fields['choice1'].choices = [
('11th'),
('2', '2nd'),
('3', '3rd'),
('4', '4th'),
('5', '5th'),
]
context = {
'form': form
}
return render(request, 'choice_sample.html', context)
(Omitted)
sample_choice_add_view = SampleChoiceAddView.as_view()
<!DOCTYPE html>
<html lang="ja">
<head>
<title> ChoiceField sample </ title>
</head>
<body>
<form method="POST" aciton="">
<select name="choice1" id="id_choice1">
<option value = "1"> 1st </ option>
<option value = "2"> second </ option>
<option value = "3"> 3rd </ option>
<option value = "4"> 4th </ option>
<option value = "5"> 5th </ option>
</select>
<input type="hidden" name="csrfmiddlewaretoken" value="xxxxxxxx">
</form>
</body>
</html>
I was able to add and go after the choices!
Recommended Posts