mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 01:26:53 +03:00
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:
parent
7b53960c3b
commit
67ebdd32cd
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user