mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
e9103402c0
Complete rework of translating data values from database Deprecation of SubfieldBase, removal of resolve_columns and convert_values in favour of a more general converter based approach and public API Field.from_db_value(). Now works seamlessly with aggregation, .values() and raw queries. Thanks to akaariai in particular for extensive advice and inspiration, also to shaib, manfre and timograham for their reviews.
33 lines
741 B
Python
33 lines
741 B
Python
import decimal
|
|
|
|
from django.db import models
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
|
|
|
|
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)
|
|
|
|
def from_db_value(self, value, connection):
|
|
cash = Cash(value)
|
|
cash.vendor = connection.vendor
|
|
return cash
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class CashModel(models.Model):
|
|
cash = CashField()
|
|
|
|
def __str__(self):
|
|
return str(self.cash)
|