-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More robust form rendering in the browsable API (#4181)
- Loading branch information
1 parent
a5f822d
commit bb22ab8
Showing
2 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from django.test import TestCase | ||
|
||
from rest_framework import generics, renderers, serializers, status | ||
from rest_framework.response import Response | ||
from rest_framework.test import APIRequestFactory | ||
from tests.models import BasicModel | ||
|
||
factory = APIRequestFactory() | ||
|
||
|
||
class BasicSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = BasicModel | ||
|
||
|
||
class ManyPostView(generics.GenericAPIView): | ||
queryset = BasicModel.objects.all() | ||
serializer_class = BasicSerializer | ||
renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer) | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(self.get_queryset(), many=True) | ||
return Response(serializer.data, status.HTTP_200_OK) | ||
|
||
|
||
class TestManyPostView(TestCase): | ||
def setUp(self): | ||
""" | ||
Create 3 BasicModel instances. | ||
""" | ||
items = ['foo', 'bar', 'baz'] | ||
for item in items: | ||
BasicModel(text=item).save() | ||
self.objects = BasicModel.objects | ||
self.data = [ | ||
{'id': obj.id, 'text': obj.text} | ||
for obj in self.objects.all() | ||
] | ||
self.view = ManyPostView.as_view() | ||
|
||
def test_post_many_post_view(self): | ||
""" | ||
POST request to a view that returns a list of objects should | ||
still successfully return the browsable API with a rendered form. | ||
Regression test for https://github.com/tomchristie/django-rest-framework/pull/3164 | ||
""" | ||
data = {} | ||
request = factory.post('/', data, format='json') | ||
with self.assertNumQueries(1): | ||
response = self.view(request).render() | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 3) |