From: Oleg B. <ph...@ph...> - 2008-07-11 14:54:12
|
On Tue, Jul 01, 2008 at 03:17:14PM -0500, Christopher Singley wrote: > class DecimalStringValidator(DecimalValidator): > def __init__(self, *args, **kw): > size = kw.pop('size', NoDefault) size is not used in the __init__(). > precision = kw.pop('precision', NoDefault) > assert precision >= 0, \ > "You must give a precision argument as a positive integer" > self.precision = Decimal(10) ** (-1 * int(precision)) > super(DecimalStringValidator, self).__init__(*args, **kw) > > def to_python(self, value, state): > value = super(DecimalStringValidator, self).to_python(value, state) > if isinstance(value, Decimal): > value = value.quantize(self.precision) > return value > > def from_python(self, value, state): > value = super(DecimalStringValidator, self).from_python(value, state) > if isinstance(value, Decimal): > value = value.quantize(self.precision) > return value from_python() is supposed to return a string - this is Decimal*String*Converter, right? Shouldn't it be implemented as def from_python(self, value, state): value = super(DecimalStringValidator, self).from_python(value, state) if isinstance(value, Decimal): value = str(value.quantize(self.precision)) # or .to_eng_string() return value ? Without str() the query is INSERT INTO test (d) VALUES (10.0000) but converting to floats is exactly what we want to prevent. With str() the query is INSERT INTO test (d) VALUES ('10.0000') Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |