Hello, I have a Decimal field that can contain a number of Decimal() values (it's V.A.T. options):
CHOIX_TVA = [
(Decimal(0), "0"),
(Decimal(5.5), "5.5 %"),
(Decimal(10), "10 %"),
(Decimal(20), "20 %"),
]
tva = models.DecimalField(max_digits=20, decimal_places=2, validators=[MinValueValidator(0), MaxValueValidator(100)])
When I load this field in a form, say :
class OpportuniteTvaForm(forms.ModelForm):
class Meta:
model = Opportunite
fields = ['tva', ]
tva = forms.ChoiceField(
required=False,
widget=forms.widgets.Select(attrs={"class": "form-select"}),
choices=Opportunite.CHOIX_TVA,
label="tva"
)
The rendered form will not have Decimal options in the select field :
<select name="tva" class="form-select" id="id_tva">
<option value="0">0</option>
<option value="5.5">5.5 %</option>
<option value="10">10 %</option>
<option value="20">20 %</option>
</select>
Saving the data seems to work, but if I load the form bound to an existing instance, then the initial data Decimal(5.5) will not match the option value="5.5", and the field is not preselected to the right option.
Here is an example of a log entry with the form's initial data:
web | 2026-02-14 14:02:05 INFO views 1288 | {'tva': Decimal('5.50')}
is it expected that forms.ChoiceField() manages the conversion from Decimal() to the HTML equivalent option, or should we handle this manually ?
Thanks for your help in advance, I hope the question is clear enough !