@@ -903,16 +903,20 @@ def to_internal_value(self, data):
903
903
904
904
# Number types...
905
905
906
- class MaxMinMixin ( object ):
906
+ class IntegerField ( Field ):
907
907
default_error_messages = {
908
+ 'invalid' : _ ('A valid integer is required.' ),
908
909
'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
909
910
'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
911
+ 'max_string_length' : _ ('String value too large.' )
910
912
}
913
+ MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
914
+ re_decimal = re .compile (r'\.0*\s*$' ) # allow e.g. '1.0' as an int, but not '1.2'
911
915
912
916
def __init__ (self , ** kwargs ):
913
917
self .max_value = kwargs .pop ('max_value' , None )
914
918
self .min_value = kwargs .pop ('min_value' , None )
915
- super (MaxMinMixin , self ).__init__ (** kwargs )
919
+ super (IntegerField , self ).__init__ (** kwargs )
916
920
if self .max_value is not None :
917
921
message = lazy (
918
922
self .error_messages ['max_value' ].format ,
@@ -926,15 +930,6 @@ def __init__(self, **kwargs):
926
930
self .validators .append (
927
931
MinValueValidator (self .min_value , message = message ))
928
932
929
-
930
- class IntegerField (MaxMinMixin , Field ):
931
- default_error_messages = {
932
- 'invalid' : _ ('A valid integer is required.' ),
933
- 'max_string_length' : _ ('String value too large.' )
934
- }
935
- MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
936
- re_decimal = re .compile (r'\.0*\s*$' ) # allow e.g. '1.0' as an int, but not '1.2'
937
-
938
933
def to_internal_value (self , data ):
939
934
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
940
935
self .fail ('max_string_length' )
@@ -949,13 +944,32 @@ def to_representation(self, value):
949
944
return int (value )
950
945
951
946
952
- class FloatField (MaxMinMixin , Field ):
947
+ class FloatField (Field ):
953
948
default_error_messages = {
954
949
'invalid' : _ ('A valid number is required.' ),
950
+ 'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
951
+ 'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
955
952
'max_string_length' : _ ('String value too large.' )
956
953
}
957
954
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
958
955
956
+ def __init__ (self , ** kwargs ):
957
+ self .max_value = kwargs .pop ('max_value' , None )
958
+ self .min_value = kwargs .pop ('min_value' , None )
959
+ super (FloatField , self ).__init__ (** kwargs )
960
+ if self .max_value is not None :
961
+ message = lazy (
962
+ self .error_messages ['max_value' ].format ,
963
+ six .text_type )(max_value = self .max_value )
964
+ self .validators .append (
965
+ MaxValueValidator (self .max_value , message = message ))
966
+ if self .min_value is not None :
967
+ message = lazy (
968
+ self .error_messages ['min_value' ].format ,
969
+ six .text_type )(min_value = self .min_value )
970
+ self .validators .append (
971
+ MinValueValidator (self .min_value , message = message ))
972
+
959
973
def to_internal_value (self , data ):
960
974
961
975
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
@@ -970,9 +984,11 @@ def to_representation(self, value):
970
984
return float (value )
971
985
972
986
973
- class DecimalField (MaxMinMixin , Field ):
987
+ class DecimalField (Field ):
974
988
default_error_messages = {
975
989
'invalid' : _ ('A valid number is required.' ),
990
+ 'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
991
+ 'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
976
992
'max_digits' : _ ('Ensure that there are no more than {max_digits} digits in total.' ),
977
993
'max_decimal_places' : _ ('Ensure that there are no more than {max_decimal_places} decimal places.' ),
978
994
'max_whole_digits' : _ ('Ensure that there are no more than {max_whole_digits} digits before the decimal point.' ),
@@ -990,12 +1006,28 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
990
1006
if self .localize :
991
1007
self .coerce_to_string = True
992
1008
1009
+ self .max_value = max_value
1010
+ self .min_value = min_value
1011
+
993
1012
if self .max_digits is not None and self .decimal_places is not None :
994
1013
self .max_whole_digits = self .max_digits - self .decimal_places
995
1014
else :
996
1015
self .max_whole_digits = None
997
1016
998
- super (DecimalField , self ).__init__ (max_value = max_value , min_value = min_value , ** kwargs )
1017
+ super (DecimalField , self ).__init__ (** kwargs )
1018
+
1019
+ if self .max_value is not None :
1020
+ message = lazy (
1021
+ self .error_messages ['max_value' ].format ,
1022
+ six .text_type )(max_value = self .max_value )
1023
+ self .validators .append (
1024
+ MaxValueValidator (self .max_value , message = message ))
1025
+ if self .min_value is not None :
1026
+ message = lazy (
1027
+ self .error_messages ['min_value' ].format ,
1028
+ six .text_type )(min_value = self .min_value )
1029
+ self .validators .append (
1030
+ MinValueValidator (self .min_value , message = message ))
999
1031
1000
1032
if rounding is not None :
1001
1033
valid_roundings = [v for k , v in vars (decimal ).items () if k .startswith ('ROUND_' )]
@@ -1319,11 +1351,30 @@ def to_representation(self, value):
1319
1351
return value .strftime (output_format )
1320
1352
1321
1353
1322
- class DurationField (MaxMinMixin , Field ):
1354
+ class DurationField (Field ):
1323
1355
default_error_messages = {
1324
1356
'invalid' : _ ('Duration has wrong format. Use one of these formats instead: {format}.' ),
1357
+ 'max_value' : _ ('Ensure this value is less than or equal to {max_value}.' ),
1358
+ 'min_value' : _ ('Ensure this value is greater than or equal to {min_value}.' ),
1325
1359
}
1326
1360
1361
+ def __init__ (self , ** kwargs ):
1362
+ self .max_value = kwargs .pop ('max_value' , None )
1363
+ self .min_value = kwargs .pop ('min_value' , None )
1364
+ super (DurationField , self ).__init__ (** kwargs )
1365
+ if self .max_value is not None :
1366
+ message = lazy (
1367
+ self .error_messages ['max_value' ].format ,
1368
+ six .text_type )(max_value = self .max_value )
1369
+ self .validators .append (
1370
+ MaxValueValidator (self .max_value , message = message ))
1371
+ if self .min_value is not None :
1372
+ message = lazy (
1373
+ self .error_messages ['min_value' ].format ,
1374
+ six .text_type )(min_value = self .min_value )
1375
+ self .validators .append (
1376
+ MinValueValidator (self .min_value , message = message ))
1377
+
1327
1378
def to_internal_value (self , value ):
1328
1379
if isinstance (value , datetime .timedelta ):
1329
1380
return value
0 commit comments