models.py 890 B

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