0

I'm newbie in Python and building API in Django using rest framework and using mysql for database. I'm using filter query to get user info object but its returning array.

In Login API my code is:

is_valid_user = users.objects.filter(email=req_email, password=req_password)
serializer = usersSerializer(is_valid_user,many=True)
    if is_valid_user:
        response = {
            'message': 'success',
            'code': 1,
            'data':serializer.data
    else:
        response = {
            'message': 'User not found',
            'code': 0,
        }
    return Response(response)

My usersSerializer Class :

class usersSerializer(serializers.ModelSerializer):
    class Meta:
        model = users
        fields = ('uid', 'first_name', 'last_name', 'email', 'gender')

For this code response is :

{
      "message": "success",
      "code": 1,
      "data": [
        {
          "uid": 6,
          "first_name": "anuj",
          "last_name": "sharma",
          "email": "[email protected]",
          "gender": "0"
        }
      ]
 }

But for this I don't want array of data . Expected result should be :

  {
    "message": "success",
    "code": 1,
    "data": {
        "uid": 6,
        "first_name": "anuj",
        "last_name": "sharma",
        "email": "[email protected]",
        "gender": "0"
    }
}

Kindly help me for this.

5
  • 1
    You're avoiding everything that django-rest-framework gives you by doing this. If you used a serializer you could easily get the result you want. Commented May 9, 2017 at 9:52
  • @DanielRoseman Thanks for your suggestion. I will read rest framework documentation for this. Commented May 9, 2017 at 9:54
  • 1
    what about isValidUser.values('uid','first_name','last_name', 'email','gender')[0] Commented May 9, 2017 at 9:54
  • @L_S I was thinking of that naive approach, too. But maybe it is an XY problem. This might be the wrong way to the solution. It'll work, however going for a more elegant solutions within the means of the framework would be the right way to go. Commented May 9, 2017 at 9:57
  • @DanielRoseman I've read about serializers and updated my code, But still getting same response. Commented May 9, 2017 at 10:58

1 Answer 1

2

The comments under your questions should point you to another solution. Here I'd like to give an explanation, why you're getting an array, and not an object.

In this line:


isValidUser = users.objects.filter(email=req_email, password=req_password)

you use the filter method, which may return more than 1 result (or none). You'll always get an array (a list in python), regardless of the number of results. The filter() method returns a new QuerySet.

If you want to retrieve a single result explicitly, and you have a unique field in your model class, then you should use the method get(), which doesn't return a QuerySet, but an object.

So if, let's say the field email is set to be unique, you could do this:


isValidUser = users.objects.get(email=req_email)

That will return an object, if there is an entry that can be matched.

Also, it is a good practice to follow the naming conventions for Python and name the variables with snake case:

is_valid_user

instead of

isValidUser
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for explaining. How can I get each and every information from that object. isValidUser.uid will give id from that object. How can I retrieve all information like id, first_name, etc in single statement ?
If you want to get the values for all properties in a single statement, then you should use a serializer. DRF offers many tools for achieving this. You can set the depth for associated model classes, use hyperlinked serializers, inherit serializers and many other things.

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.