Skip to content

Commit 1f10a39

Browse files
author
Ryan Allen
committed
Match IntegerField validation with Django's, preventing decimal values being stored as ints, fixes #2835.
Match IntegerField validation with Django IntegerField, prevents decimal values being stored as ints, fixes #2835 On branch master
1 parent ecb37f5 commit 1f10a39

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

rest_framework/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def to_internal_value(self, data):
682682
self.fail('max_string_length')
683683

684684
try:
685-
data = int(data)
685+
data = int(re.compile(r'\.0*\s*$').sub('', str(data)))
686686
except (ValueError, TypeError):
687687
self.fail('invalid')
688688
return data

tests/test_fields.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,13 @@ class TestIntegerField(FieldValues):
549549
1: 1,
550550
0: 0,
551551
1.0: 1,
552-
0.0: 0
552+
0.0: 0,
553+
'1.0': 1
553554
}
554555
invalid_inputs = {
555-
'abc': ['A valid integer is required.']
556+
0.5: ['A valid integer is required.'],
557+
'abc': ['A valid integer is required.'],
558+
'0.5': ['A valid integer is required.']
556559
}
557560
outputs = {
558561
'1': 1,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy