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

Commit79be20a

Browse files
TigorClovelydinosaur
authored andcommitted
Updated supported values for the NullBooleanField (#5387)
* Updated supported values for the NullBooleanField.* Added check for unhashable types in NullBooleanField.
1 parente42eb42 commit79be20a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

‎rest_framework/fields.py‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,22 @@ class NullBooleanField(Field):
688688
'invalid':_('"{input}" is not a valid boolean.')
689689
}
690690
initial=None
691-
TRUE_VALUES= {'t','T','true','True','TRUE','1',1,True}
692-
FALSE_VALUES= {'f','F','false','False','FALSE','0',0,0.0,False}
691+
TRUE_VALUES= {
692+
't','T',
693+
'y','Y','yes','YES',
694+
'true','True','TRUE',
695+
'on','On','ON',
696+
'1',1,
697+
True
698+
}
699+
FALSE_VALUES= {
700+
'f','F',
701+
'n','N','no','NO',
702+
'false','False','FALSE',
703+
'off','Off','OFF',
704+
'0',0,0.0,
705+
False
706+
}
693707
NULL_VALUES= {'n','N','null','Null','NULL','',None}
694708

695709
def__init__(self,**kwargs):
@@ -698,12 +712,15 @@ def __init__(self, **kwargs):
698712
super(NullBooleanField,self).__init__(**kwargs)
699713

700714
defto_internal_value(self,data):
701-
ifdatainself.TRUE_VALUES:
702-
returnTrue
703-
elifdatainself.FALSE_VALUES:
704-
returnFalse
705-
elifdatainself.NULL_VALUES:
706-
returnNone
715+
try:
716+
ifdatainself.TRUE_VALUES:
717+
returnTrue
718+
elifdatainself.FALSE_VALUES:
719+
returnFalse
720+
elifdatainself.NULL_VALUES:
721+
returnNone
722+
exceptTypeError:# Input is an unhashable type
723+
pass
707724
self.fail('invalid',input=data)
708725

709726
defto_representation(self,value):

‎tests/test_fields.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,15 @@ def test_disallow_unhashable_collection_types(self):
587587
[],
588588
{},
589589
)
590-
field=serializers.BooleanField()
590+
field=self.field
591591
forinput_valueininputs:
592592
withpytest.raises(serializers.ValidationError)asexc_info:
593593
field.run_validation(input_value)
594594
expected= ['"{0}" is not a valid boolean.'.format(input_value)]
595595
assertexc_info.value.detail==expected
596596

597597

598-
classTestNullBooleanField(FieldValues):
598+
classTestNullBooleanField(TestBooleanField):
599599
"""
600600
Valid and invalid values for `BooleanField`.
601601
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp