[Answered ]-Getting Django current user on iOS app

1👍

For everyone trying to get the profile for a user this is the way I managed to do it thanks to Kevin’s help.

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        depth = 1
        fields = ('id','username','email','profile')



class CurrentUserView(APIView):
    def get(self, request):
        serializer = UserSerializer(request.user)
        return Response(serializer.data)

Using a url like this

url(r'^api/current-user',CurrentUserView.as_view(),
        name="current_user"),

You will get an answer like this

"id": 1, 
    "username": "username", 
    "email": "email@email.com", 
    "profile": {
        "id": 1, 
        "user": 1, 
        "bio": "My Bio", 
        "sex": "M", 
        "birthdate": "1987-12-02"
    }

1👍

You didn’t post your ViewSet code, so I have to guess. But your model should be set to User and your lookup_field should be set to username.

Something like:

class UserViewSet(ModelViewSet):
    model = User
    lookup_field = "username"

Leave a comment