| 463 | super().__init__(**kwargs) |
| 464 | |
| 465 | def to_python(self, value): |
| 466 | # Convert to string for python 2.6 before casting to Decimal |
| 467 | try: |
| 468 | value = decimal.Decimal("%s" % value) |
| 469 | except (TypeError, ValueError, decimal.InvalidOperation): |
| 470 | return value |
| 471 | if self.precision > 0: |
| 472 | return value.quantize( |
| 473 | decimal.Decimal(".%s" % ("0" * self.precision)), rounding=self.rounding |
| 474 | ) |
| 475 | else: |
| 476 | return value.quantize(decimal.Decimal(), rounding=self.rounding) |
| 477 | |
| 478 | def to_mongo(self, value): |
| 479 | if self.force_string: |