models.py 750 B

1234567891011121314151617181920212223242526272829303132
  1. import decimal
  2. from django.db import models
  3. from django.utils.encoding import python_2_unicode_compatible
  4. class Cash(decimal.Decimal):
  5. currency = 'USD'
  6. def __str__(self):
  7. s = super(Cash, self).__str__(self)
  8. return '%s %s' % (s, self.currency)
  9. class CashField(models.DecimalField):
  10. def __init__(self, **kwargs):
  11. kwargs['max_digits'] = 20
  12. kwargs['decimal_places'] = 2
  13. super(CashField, self).__init__(**kwargs)
  14. def from_db_value(self, value, connection, context):
  15. cash = Cash(value)
  16. cash.vendor = connection.vendor
  17. return cash
  18. @python_2_unicode_compatible
  19. class CashModel(models.Model):
  20. cash = CashField()
  21. def __str__(self):
  22. return str(self.cash)