Reject PrimaryKeyRelatedField bool lookup values (#7597)

* Reject PrimaryKeyRelatedField bool lookup values

* Test PrimaryKeyRelatedField bool lookup rejection

* Fix indentation in test
This commit is contained in:
Aristotelis Mikropoulos 2021-03-17 15:28:38 +02:00 committed by GitHub
parent 7b53960c3b
commit 67ebdd32cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -259,6 +259,8 @@ class PrimaryKeyRelatedField(RelatedField):
data = self.pk_field.to_internal_value(data)
queryset = self.get_queryset()
try:
if isinstance(data, bool):
raise TypeError
return queryset.get(pk=data)
except ObjectDoesNotExist:
self.fail('does_not_exist', pk_value=data)

View File

@ -107,6 +107,12 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase):
msg = excinfo.value.detail[0]
assert msg == 'Incorrect type. Expected pk value, received BadType.'
def test_pk_related_lookup_bool(self):
with pytest.raises(serializers.ValidationError) as excinfo:
self.field.to_internal_value(True)
msg = excinfo.value.detail[0]
assert msg == 'Incorrect type. Expected pk value, received bool.'
def test_pk_representation(self):
representation = self.field.to_representation(self.instance)
assert representation == self.instance.pk