Django Boolean Field returning “---------” as value
-
29-06-2021 - |
Question
I'm creating a form based on a model, where I have a boolean field as this:
project_type = models.CharField(max_length=50, choices=JOB_TYPES)
Choices are:
JOB_TYPES = (
('fulltime', _('Fulltid')),
('project', _('Prosjektbasert')),
)
To display the input type as radio buttons rather than , I'm doing this:
class AddJob(ModelForm):
class Meta:
model = Jobs
widgets = {
'project_type': RadioSelect(),
}
In the HTML, the form output looks like this:
<label for="id_for_project_type_0">Project type</label>
<ul>
<li><label for="id_for_project_type_0"><input checked="checked" type="radio" id="id_for_project_type_0" value="" name="project_type" /> ---------</label></li>
<li><label for="id_for_project_type_1"><input type="radio" id="id_for_project_type_1" value="fulltime" name="project_type" /> Fulltid</label></li>
<li><label for="id_for_project_type_2"><input type="radio" id="id_for_project_type_2" value="project" name="project_type" /> Prosjektbasert</label></li>
</ul>
Why am I getting a value 0? I've only specified two values in the model. And how do I get rid of it?
Thanks in advance :)
Solution
You need to specify a default value for project_type
:
project_type = models.CharField(max_length=50, choices=JOB_TYPES,
default=JOB_TYPES[0][0])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow