Integer based MoneyField

 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

  1. django-pyodbc MoneyField by Tarken 5 years, 5 months ago
  2. Widget for Money values on Geraldo Reports by marinho 5 years ago
  3. Fixed-point Decimal model field with integer storage by lanzz 1 month, 1 week ago
  4. 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 2 years, 1 month ago
  5. Currency Fields with newforms by sago 7 years ago

Comments

(Forgotten your password?)