Sorry for such a lame question, but I can't set an initial value for a field in ModelForm. It looks like this:
class OrderRegisterForm(forms.ModelForm):
class Meta:
model = Order
widgets = {
'weight_units': forms.RadioSelect,
}
And in the model:
class Order(models.Model):
WEIGHT_UNIT_CHOICES = (
('KG', 'kg'),
('TN', 't'),
)
weight_units = models.CharField(
max_length=2,
choices=WEIGHT_UNIT_CHOICES,
default="KG"
)
But then I'm trying to render it in a template like this
<ul class="form__row__list">
{{ order_form.weight_units }}
</ul>
correct values appear, but radio button with default value is not selected. I tried to pass an initial argument, override __init__
in form and so on, but still nothing works.
UPDATE
as_p() provides the correct result. Things are getting weird.
<p>
<label for="id_weight_units_0">Weight units:</label>
<ul id="id_weight_units">
<li>
<label for="id_weight_units_0"><input checked="checked" id="id_weight_units_0" name="weight_units" type="radio" value="KG" /> kg</label>
</li>
<li>
<label for="id_weight_units_1"><input id="id_weight_units_1" name="weight_units" type="radio" value="TN" /> t</label>
</li>
</ul>
Please, show me my mistake.