1

Need to preform simple check: If user exist return True, False if not.I am trying to solve thise problem using custom json answers, not sure it is the right way to deal with it. Please help me .

My serializer:

User = get_user_model()

Class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (User.USERNAME_FIELD, )

My views:

@api_view(['GET'])
def UserViewSet(self, request):
    if request.method == 'GET':
        try:
            user = User.objects.get(username=User.USERNAME_FIELD)
            if User.DoesNotExist:
                json = {}
                json['message']= 'False'
                return Response(json)
            else:
                json = {}
                json['message']= 'True'
                return Response(json)
5
  • Please explain what you are trying to do in this line User.objects.get(username=User.USERNAME_FIELD). Commented Apr 16, 2016 at 17:26
  • Also, you can use RetrieveAPIView to decide if object exists or not. If 404 status code is returned, it means user does not exist. Otherwise 200 status code will be returned Commented Apr 16, 2016 at 17:33
  • trying get username with that line Commented Apr 16, 2016 at 17:36
  • But this query will always be User.objects.get(username='field_name'). How will this retrieve intended unique user? Commented Apr 16, 2016 at 17:38
  • the thing is i should get True or False insetead of 404 or 200 exceptions. Commented Apr 16, 2016 at 17:45

1 Answer 1

5

Instead of using UserViewSet and UserSerializer, you can just use APIView for that.

class UserExistsView(APIView):

    def get(self, request, *args, **kwargs):
        # use this if username is in url kwargs
        username = self.kwargs.get('username') 

        # use this if username is being sent as a query parameter
        username = self.request.query_params.get('username')  

        try:
            user = User.objects.get(username=username) # retrieve the user using username
        except User.DoesNotExist:
            return Response(data={'message':False}) # return false as user does not exist
        else:
            return Response(data={'message':True}) # Otherwise, return True
Sign up to request clarification or add additional context in comments.

5 Comments

how in this case query should look like? User.objects.get(self.kwargs['username']) ?
How are you sending the username of a particular user? Is it in url kwargs or as a query parameter?
regular i use next contruction to return some filtered data - def get_queryset(self): user = self.kwargs['username'] return User.objects.filter[username=user]
Updated the ans with 2 options to get the username and then perform a lookup based on it.
I this is very old...but i tried this but i am getting empty response using apisauce. i have asked the question here.Kindly assist stackoverflow.com/questions/64624712/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.