Simplify conversion of int and float fields

This commit is contained in:
Itai Shirav 2016-06-30 16:17:53 +03:00
parent 4efdcda97b
commit a3bd212133

View File

@ -108,11 +108,10 @@ class DateTimeField(Field):
class BaseIntField(Field):
def to_python(self, value):
if isinstance(value, int):
return value
if isinstance(value, basestring):
try:
return int(value)
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
except:
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
def validate(self, value):
self._range_check(value, self.min_value, self.max_value)
@ -177,11 +176,10 @@ class Int64Field(BaseIntField):
class BaseFloatField(Field):
def to_python(self, value):
if isinstance(value, float):
return value
if isinstance(value, basestring) or isinstance(value, int):
try:
return float(value)
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
except:
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
class Float32Field(BaseFloatField):