Django Rest required parameters in URL
<p>I'm using django rest framework.
Here is my code:</p>

<p><strong>urls.py:</strong></p>

<pre><code>urlpatterns = [
    url(r'^users/show', UserShow.as_view()),
]
</code></pre>

<p><strong>view.py:</strong></p>

<pre><code>class UserShow(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def get_queryset(self):
        queryset = User.objects.all()
        username = self.request.query_params.get('username', None)
        user_id = self.request.query_params.get('user_id', None)
        if username is not None:
            queryset = queryset.filter(username=username)
        if user_id is not None:
            queryset = queryset.filter(pk=user_id)
        return queryset
</code></pre>

<p>I want to get values from url like this: 
<code>/users/show?user_id=1</code> or <code>/users/show?username=mike</code>.</p>

<p><strong>Either an <code>user_id</code> or <code>username</code> must be required parameter.</strong> How can I control it in class based views? </p>

<p>With my code if I'm sending the request with wrong parameter name <code>/users/show?user111name=mike</code> or simple <code>/users/show</code> the view of course response me with <code>queryset = User.objects.all()</code> and lists all the users. I don't need that. I need if required parameters are <code>None</code> response with 404.</p>

<p>I can get needed result with <strong>function based view</strong>:</p>

<pre><code>@api_view(['GET'])
def users(request):
    if request.method == 'GET':
        queryset = User.objects.all()
        username = request.GET.get('username', None)
        user_id = request.GET.get('user_id', None)

        if username is not None:
            queryset = queryset.filter(username=username)
        elif user_id is not None:
            queryset = queryset.filter(pk=user_id)
        else:
            return Response({"status": "required field not found."},
                            status=status.HTTP_404_NOT_FOUND)

        if not queryset.exists():
            return Response({"status": "not found."},
                            status=status.HTTP_404_NOT_FOUND)

        serializer = UserSerializer(queryset, many=True)
        return Response(serializer.data)
</code></pre>

<p>But how can I do it with <strong>generic</strong> class based views?</p>