0

In my python rest api application, I have successfully included the django oauth2 toolkit and I am getting accesstoken while logging in. Now I have to write an api to get the details of the current logged in user and his group information and the group permissions.

My urls.py

url(r'me', get_me),

My serialzers.py

class UserGroupSerializer(serializers.ModelSerializer):
    permissions = serializers.SerializerMethodField(required=False)
    groups = serializers.SerializerMethodField(required=False)

    def get_groups(self, obj):
        group_list = []
        for group in obj.groups.all():
            group_list.append(group.name)
        return group_list


    def get_permissions(self, obj):
        permissions = []
        for group in obj.groups.all():
            group_permissions = group.permissions.all().values_list('codename', flat=True)
            for group_permission in group_permissions:
                permissions.append(group_permission)
        return permissions

    class Meta:
        model = get_user_model()
        exclude = ('password','user_permissions',)
        #fields = '__all__'

My views.py

def get_me(request):
    user = request.user
    return {
       'user': UserGroupSerializer(user).data,
    }

When I call this api, I am getting the following error,

The view account.views.get_me didn't return an HttpResponse object. It returned None instead.

What is the issue? Is it the correct method am following?

Arun SS
  • 1,791
  • 8
  • 29
  • 48

1 Answers1

1

You must return a value with HttpResponse or another response function for your get_me function.

DRF way of doing this:

from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view

@api_view(['GET'])
def my_funct(request):
        user = request.user
        return Response({'user': UserGroupSerializer(user).data}, status=status.HTTP_200_OK)
Umut Gunebakan
  • 391
  • 3
  • 14
  • Thats not working. showing this error AssertionError at /api/v1/me .accepted_renderer not set on Response – Arun SS Jan 24 '17 at 09:20
  • Yes. It worked. Thanks. Is it the best way to get the user details and permissions? – Arun SS Jan 24 '17 at 11:29
  • Groups and Permissions are kept as models in DB. I would first create group and permission serializers first, then I will add those serializers into UserGroupSerializer. At the end of the day, you will see nested groups and permissons in a JSON array as a response. – Umut Gunebakan Jan 24 '17 at 19:59