arstj arstj - 6 months ago 23
Python Question

Django Rest required parameters in URL

I'm using django rest framework.
Here is my code:

urls.py:

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


view.py:

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


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

Either an
user_id
or
username
must be required parameter.
How can I control it in class based views?

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

I can get needed result with function based view:

@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)


But how can I do it with generic class based views?

Answer
class UserShow(ListAPIView):

    queryset = User.objects.all()
    serializer_class = UserSerializer

    def filter_queryset(self, queryset):
        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

    def list(self,request,*args,**kwargs):
        username = self.request.query_params.get('username', None)
        user_id = self.request.query_params.get('user_id', None)
        if not (username or user_id):
            return Response({"status": "Required field not found."},
                                        status=status.HTTP_404_NOT_FOUND)
        return super(UserShow, self).list(request,*args,**kwargs)