I looked through every single similar question on stackoverflow and tried nearly everything.
It seems easy to do: I just want to allow , as decimal separator for a FloatField in the Django admin interface. At the moment, it depends on the localization, but I always want to allow it. It would even be ok for me, if it's just a TextInput, but I need , to work. Setting DECIMAL_SEPARATOR in settings.py does not work.
My question is similar to this 6 year old, unanswered one: How to only override DECIMAL_SEPARATOR in django while keeping localization intact?
I managed to use the TextInput widget for FloatFields like this:
class ExampleAdminForm(forms.ModelForm):
class Meta:
model = Example
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for key, value in self.fields.items():
if isinstance(value, FloatField):
self.fields[key].widget = TextInput()
The widget works, but an input like 1,23 leads to an error message Enter a number. I'd have to implement my own, custom FloatField that overrides the sanitizing in to_python to allow for differenct decimal separators, but then I'd still need a different widget. This seems awfully complicated, is there a better way?
If not, how can I change the frontend validation in the widget to ignore localization and use my own regex pattern?