2014-08-12 12:08:40 +00:00
|
|
|
import decimal
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class Cash(decimal.Decimal):
|
|
|
|
currency = 'USD'
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
s = super().__str__(self)
|
2014-08-12 12:08:40 +00:00
|
|
|
return '%s %s' % (s, self.currency)
|
|
|
|
|
|
|
|
|
|
|
|
class CashField(models.DecimalField):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
kwargs['max_digits'] = 20
|
|
|
|
kwargs['decimal_places'] = 2
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(**kwargs)
|
2014-08-12 12:08:40 +00:00
|
|
|
|
2017-07-06 17:18:05 +00:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2014-08-12 12:08:40 +00:00
|
|
|
cash = Cash(value)
|
|
|
|
cash.vendor = connection.vendor
|
|
|
|
return cash
|
|
|
|
|
|
|
|
|
|
|
|
class CashModel(models.Model):
|
|
|
|
cash = CashField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.cash)
|
2017-07-06 17:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CashFieldDeprecated(CashField):
|
|
|
|
def from_db_value(self, value, expression, connection, context):
|
|
|
|
return super().from_db_value(value, expression, connection)
|
|
|
|
|
|
|
|
|
|
|
|
class CashModelDeprecated(models.Model):
|
|
|
|
cash = CashFieldDeprecated()
|