Do not list related field choices in OPTIONS requests.

Do not list related field choices in OPTIONS requests.
This commit is contained in:
Simon Charette 2016-06-01 11:33:03 -04:00 committed by Tom Christie
parent 9d5773772b
commit 014e24b024
2 changed files with 17 additions and 2 deletions

View File

@ -137,7 +137,9 @@ class SimpleMetadata(BaseMetadata):
elif getattr(field, 'fields', None): elif getattr(field, 'fields', None):
field_info['children'] = self.get_serializer_info(field) field_info['children'] = self.get_serializer_info(field)
if not field_info.get('read_only') and hasattr(field, 'choices'): if (not field_info.get('read_only') and
not isinstance(field, serializers.RelatedField) and
hasattr(field, 'choices')):
field_info['choices'] = [ field_info['choices'] = [
{ {
'value': choice_value, 'value': choice_value,

View File

@ -11,6 +11,8 @@ from rest_framework.renderers import BrowsableAPIRenderer
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory
from .models import BasicModel
request = Request(APIRequestFactory().options('/')) request = Request(APIRequestFactory().options('/'))
@ -261,10 +263,21 @@ class TestMetadata:
view = ExampleView.as_view(versioning_class=scheme) view = ExampleView.as_view(versioning_class=scheme)
view(request=request) view(request=request)
class TestSimpleMetadataFieldInfo(TestCase):
def test_null_boolean_field_info_type(self): def test_null_boolean_field_info_type(self):
options = metadata.SimpleMetadata() options = metadata.SimpleMetadata()
field_info = options.get_field_info(serializers.NullBooleanField()) field_info = options.get_field_info(serializers.NullBooleanField())
assert field_info['type'] == 'boolean' self.assertEqual(field_info['type'], 'boolean')
def test_related_field_choices(self):
options = metadata.SimpleMetadata()
BasicModel.objects.create()
with self.assertNumQueries(0):
field_info = options.get_field_info(
serializers.RelatedField(queryset=BasicModel.objects.all())
)
self.assertNotIn('choices', field_info)
class TestModelSerializerMetadata(TestCase): class TestModelSerializerMetadata(TestCase):