mercredi 12 juillet 2017

django rest detail_route testing

Good day!
I have a view with detail_route like this:

class PostView(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer

@detail_route(methods=['POST'])
def like(self, request, pk=None):
    post = self.get_object()
    post.like(request.user)
    return Response({'result': 'success'})

So, url for like function is /api/posts/{id}/like

I try testing it with django.test.TestCase like this:

post = Post.objects.first()
url = reverse('api:post-detail', args=[post.id])
url = urljoin(url, 'like')
response = self.client.post(url, content_type='application/json', follow=True)

I have to use follow=True because I get code 300 redirect, but redirect return me GET request when I need POST. I have tried use APIClient and APIRequestFactory and got same error or myapp.models.DoesNotExist
Tanks for your attention!

Aucun commentaire:

Enregistrer un commentaire