This might be handy in countries where decimals are entered with a comma separating the decimal places from the integer part (for instance in Germany). It lets user enter and displays all decimals with a comma separator.
I ran into this problem and couldn't find a clean internationalized way of doing it... but newforms makes it so easy to roll your own. Hope it helps someone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from django import newforms as forms from django.utils.encoding import smart_str class CommaWidget(forms.widgets.TextInput): def render(self, name, value, attrs=None): return super(CommaWidget, self).render(name, smart_str(value).replace('.', ',')) class CommaDecimalField(forms.DecimalField): """ Extension to DecimalField that allows comma-separated Decimals to be entered and displayed """ widget = CommaWidget def clean(self, value): value = smart_str(value).replace(',', '.') return super(CommaDecimalField, self).clean(value)
More like this
- FileField having auto upload_to path by junaidmgithub 15 hours, 2 minutes ago
- LazyPrimaryKeyRelatedField by LLyaudet 1 week, 1 day ago
- CacheInDictManager by LLyaudet 1 week, 1 day ago
- MYSQL Full Text Expression by Bidaya0 1 week, 2 days ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 1 day ago