Django REST Custom View Parameters in Viewset with CoreAPI
<p>If I define a django-rest-framework view framework as:</p>

<pre><code>class ProblemViewSet(viewsets.ModelViewSet):
    queryset = Problem.objects.all()
    serializer_class = ProblemSerializer

    @detail_route(methods=['post'], permission_classes=(permissions.IsAuthenticated,))
    def submit(self, request, *args, **kwargs):
        # my code here
        return Response(...)
</code></pre>

<p>When I look at the CoreAPI schema that is defined, I find this:</p>

<pre><code>problems: {
    create(title, text, value, category, hint)
    destroy(pk)
    list([page])
    partial_update(pk, [title], [text], [value], [category], [hint])
    retrieve(pk)
    submit(pk)
    update(pk, title, text, value, category, hint)
}
</code></pre>

<p>I'd like the <code>submit</code> API endpoint to take an additional parameter, called <code>answer</code>, but so far I haven't figured out how to add such a custom parameter. I know I can just pass in the POST array, but that seems inelegant and un-RESTful. Any ideas?</p>

<p>I don't understand your question, but I think that you want to add in the input the filed <code>answer</code> to django-rest form or raw, you can do this adding in the serializer <code>ProblemSerializer</code> the next:</p>

<pre><code>from rest_framework import serializers
...
class CustomerSerializer(serializers.ModelSerializer):
    answer = serializers.SerializerMethodField()
    class Meta:
        model = Problem
        fields = ['answer', 'my-normal-params',...]

def get_answer(self, problem):
    if hasattr(problem, 'answer'):
        request = self.context['request']
        return answer
</code></pre>

<p>In get_answer method you will display in the json the value that you want, or you can return None (null) if you want, </p>

<p>If that is not your problem, please say me, I'll help you.</p>

python django rest django-rest-framework