mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Removed the DynamicSerializerView duplication
This commit is contained in:
parent
128bda5712
commit
88f5921f2f
|
@ -508,6 +508,25 @@ class ExclusiveFilterBackend(object):
|
|||
return queryset.filter(text='other')
|
||||
|
||||
|
||||
class TwoFieldModel(models.Model):
|
||||
field_a = models.CharField(max_length=100)
|
||||
field_b = models.CharField(max_length=100)
|
||||
|
||||
|
||||
class DynamicSerializerView(generics.ListCreateAPIView):
|
||||
model = TwoFieldModel
|
||||
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer)
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'POST':
|
||||
class DynamicSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TwoFieldModel
|
||||
fields = ('field_b',)
|
||||
return DynamicSerializer
|
||||
return super(DynamicSerializerView, self).get_serializer_class()
|
||||
|
||||
|
||||
class TestFilterBackendAppliedToViews(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
@ -564,28 +583,6 @@ class TestFilterBackendAppliedToViews(TestCase):
|
|||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data, {'id': 1, 'text': 'foo'})
|
||||
|
||||
|
||||
class TwoFieldModel(models.Model):
|
||||
field_a = models.CharField(max_length=100)
|
||||
field_b = models.CharField(max_length=100)
|
||||
|
||||
|
||||
class DynamicSerializerView(generics.ListCreateAPIView):
|
||||
model = TwoFieldModel
|
||||
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer)
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'POST':
|
||||
class DynamicSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TwoFieldModel
|
||||
fields = ('field_b',)
|
||||
return DynamicSerializer
|
||||
return super(DynamicSerializerView, self).get_serializer_class()
|
||||
|
||||
|
||||
class TestFilterBackendAppliedToViews(TestCase):
|
||||
|
||||
def test_dynamic_serializer_form_in_browsable_api(self):
|
||||
"""
|
||||
GET requests to ListCreateAPIView should return filtered list.
|
||||
|
|
Loading…
Reference in New Issue
Block a user