This accepts values such as $1,000,000 and stores them to the database as integers. It also re-renders them to the screen using the django.contrib.humanize.intcomma method which takes 1000000 and turns it into 1,000,000. Useful for large currency fields where the decimals aren't really necessary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class IntegerCurrencyInput(forms.TextInput):
def render(self, name, value, attrs=None):
from django.contrib.humanize.templatetags.humanize import intcomma
if value:
value = "$%s" % intcomma(value)
return super(IntegerCurrencyInput, self).render(name, value, attrs)
class IntegerCurrencyField(forms.IntegerField):
widget = IntegerCurrencyInput
def clean(self, value):
if value:
if value[0] == "$": value = value[1:] # Cut off the dollar sign
value = value.replace(',', '') # Remove Commas
value = super(IntegerCurrencyField, self).clean(value)
return int(value) if value else value
|
More like this
- Browser-native date input field by kytta 1 month, 1 week ago
- Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
- My firs Snippets by GutemaG 1 month, 3 weeks ago
- FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 3 months ago
Comments
Please login first before commenting.