def to_python(self, value): """ Excerpt from `Django doc <https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.Field.to_python>`__: As a general rule, the method should deal gracefully with any of the following arguments: - An instance of the correct type (e.g., Hand in our ongoing example). - A string (e.g., from a deserializer). - Whatever the database returns for the column type you’re using. I'd add "Any value specified for this field when instantiating a model." >>> to_python(None) >>> to_python(30) >>> to_python(30L) >>> to_python('') >>> to_python(Decimal(0)) """ if isinstance(value, Decimal): return value if value: if isinstance(value, basestring): return quantities.parse(value) return Decimal(value) return None
def to_python(self, value): """ Excerpt from `Django docs <https://docs.djangoproject.com/en/1.11/howto/custom-model-fields/#converting-values-to-python-objects>`__: As a general rule, the method should deal gracefully with any of the following arguments: - An instance of the correct type (e.g., Hand in our ongoing example). - A string (e.g., from a deserializer). - Whatever the database returns for the column type you’re using. I'd add "Any value potentially specified for this field when instantiating a model." >>> to_python(None) >>> to_python(30) >>> to_python(30L) >>> to_python('') >>> to_python(Decimal(0)) """ if isinstance(value, Decimal): return value if value: # try: if isinstance(value, six.string_types): return quantities.parse(value) return Decimal(value) # except Exception as e: # raise ValidationError( # "Invalid value {} for {} : {}".format(value, self, e)) return None
def to_python(self, value): """ Excerpt from `Django doc <https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.Field.to_python>`__: As a general rule, the method should deal gracefully with any of the following arguments: - An instance of the correct type (e.g., Hand in our ongoing example). - A string (e.g., from a deserializer). - Whatever the database returns for the column type you’re using. I'd add "Any value specified for this field when instantiating a model." >>> to_python(None) >>> to_python(30) >>> to_python(30L) >>> to_python('') >>> to_python(Decimal(0)) """ if isinstance(value, Decimal): return value if value: if isinstance(value, six.string_types): return quantities.parse(value) return Decimal(value) return None
def from_db_value(self, value, expression, connection, context): return quantities.parse(value) if value else self.get_default()