Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51265fa

Browse files
author
ismaeljs
committed
add warning when min_value and max_value are not decimal
1 parente08e606 commit51265fa

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

‎rest_framework/fields.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importdecimal
55
importfunctools
66
importinspect
7+
importlogging
78
importre
89
importuuid
910
fromcollections.abcimportMapping
@@ -38,6 +39,8 @@
3839
fromrest_framework.utils.timezoneimportvalid_datetime
3940
fromrest_framework.validatorsimportProhibitSurrogateCharactersValidator
4041

42+
logger=logging.getLogger("rest_framework.fields")
43+
4144

4245
classempty:
4346
"""
@@ -990,6 +993,11 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
990993
self.max_value=max_value
991994
self.min_value=min_value
992995

996+
ifself.max_valueisnotNoneandnotisinstance(self.max_value,decimal.Decimal):
997+
logger.warning("max_value in fields.DecimalField should be Decimal type.")
998+
ifself.min_valueisnotNoneandnotisinstance(self.min_value,decimal.Decimal):
999+
logger.warning("min_value in fields.DecimalField should be Decimal type.")
1000+
9931001
ifself.max_digitsisnotNoneandself.decimal_placesisnotNone:
9941002
self.max_whole_digits=self.max_digits-self.decimal_places
9951003
else:

‎tests/test_fields.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,17 @@ class TestMinMaxDecimalField(FieldValues):
12161216
min_value=10,max_value=20
12171217
)
12181218

1219+
deftest_warning_when_not_decimal_types(self,caplog):
1220+
importlogging
1221+
serializers.DecimalField(
1222+
max_digits=3,decimal_places=1,
1223+
min_value=10,max_value=20
1224+
)
1225+
assertcaplog.record_tuples== [
1226+
("rest_framework.fields",logging.WARNING,"max_value in fields.DecimalField should be Decimal type."),
1227+
("rest_framework.fields",logging.WARNING,"min_value in fields.DecimalField should be Decimal type.")
1228+
]
1229+
12191230

12201231
classTestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
12211232
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp