1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from django.db import models
class MoneyField(models.IntegerField):
__metaclass__ = models.SubfieldBase
def get_db_prep_value(self, value):
if value is None:
return None
return int(value * 100)
def to_python(self, value):
if value is None or isinstance(value, float):
return value
try:
return float(value) / 100
except (TypeError, ValueError):
raise exceptions.ValidationError(
"This value must be an integer or a string represents an integer.")
def formfield(self, **kwargs):
from django.forms import FloatField
defaults = {'form_class': FloatField}
defaults.update(kwargs)
return super(MoneyField, self).formfield(**defaults)
|
More like this
- django-pyodbc MoneyField by Tarken 4 years, 6 months ago
- Converts an integer or floating-point number or a string to a string containing the delimiter character (default comma) after every delimeter_count digits (by default 3 digits) by pikhovkin 1 year, 2 months ago
- Currency Fields with newforms by sago 6 years, 1 month ago
- Roman Numeral Filter by Paperface 6 years, 1 month ago
- Getting a new ID according to the content type by rodnsi 4 years ago
Comments