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 3 years, 3 months ago
  2. Currency Fields with newforms by sago 4 years, 10 months ago
  3. Generate Model Data. Lots of Options. by bl4th3rsk1t3 1 year, 10 months ago
  4. Widget for Money values on Geraldo Reports by marinho 2 years, 9 months ago
  5. Using descriptors for lazy attribute caching by djypsy 4 years, 6 months ago

Comments

(Forgotten your password?)