2014-08-12 20:08:40 +08:00
|
|
|
import decimal
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class Cash(decimal.Decimal):
|
|
|
|
currency = 'USD'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = super(Cash, self).__str__(self)
|
|
|
|
return '%s %s' % (s, self.currency)
|
|
|
|
|
|
|
|
|
|
|
|
class CashField(models.DecimalField):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
kwargs['max_digits'] = 20
|
|
|
|
kwargs['decimal_places'] = 2
|
|
|
|
super(CashField, self).__init__(**kwargs)
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2014-08-12 20:08:40 +08:00
|
|
|
cash = Cash(value)
|
|
|
|
cash.vendor = connection.vendor
|
|
|
|
return cash
|
|
|
|
|
|
|
|
|
|
|
|
class CashModel(models.Model):
|
|
|
|
cash = CashField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.cash)
|