Skip to content

Commit 67ebdd3

Browse files
authored
Reject PrimaryKeyRelatedField bool lookup values (encode#7597)
* Reject PrimaryKeyRelatedField bool lookup values * Test PrimaryKeyRelatedField bool lookup rejection * Fix indentation in test
1 parent 7b53960 commit 67ebdd3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

rest_framework/relations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def to_internal_value(self, data):
259259
data = self.pk_field.to_internal_value(data)
260260
queryset = self.get_queryset()
261261
try:
262+
if isinstance(data, bool):
263+
raise TypeError
262264
return queryset.get(pk=data)
263265
except ObjectDoesNotExist:
264266
self.fail('does_not_exist', pk_value=data)

tests/test_relations.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def test_pk_related_lookup_invalid_type(self):
107107
msg = excinfo.value.detail[0]
108108
assert msg == 'Incorrect type. Expected pk value, received BadType.'
109109

110+
def test_pk_related_lookup_bool(self):
111+
with pytest.raises(serializers.ValidationError) as excinfo:
112+
self.field.to_internal_value(True)
113+
msg = excinfo.value.detail[0]
114+
assert msg == 'Incorrect type. Expected pk value, received bool.'
115+
110116
def test_pk_representation(self):
111117
representation = self.field.to_representation(self.instance)
112118
assert representation == self.instance.pk

0 commit comments

Comments
 (0)