Piyush
Piyush

Reputation: 599

How to get the user value while accessing the rest api in DRF?

I am trying to get the user value in the get request of the api call so that i can make available the list of object which user has permission to view.

I have a user object in DRF as :

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    userType = models.IntegerField(default=0)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    objects = UserManager()

    USERNAME_FIELD = 'email'

    def __str__(self):
        return self.name

I need to get the user type here in the get request here :

class getAppCPHList(APIView):
    authentication_classes = (authentication.BasicAuthentication,)
    permission_classes = (permissions.IsAuthenticated)

    def get(self, request, *args, **kwargs):
        param = {"search": "%*(^&^()*&^%^)^$&$NewModel", "limit":35000}
        data = requests.get(url = settings.NODEIP, params = param ).json()
        #Is user type is is_staff or not ? 
        newArray = []
        if not data:
            newArray = []
        else:
            for x in data:
                if x['data'].get('allSearch','') == "%*(^&^()*&^%^)^$&$NewModel":
                    if x['data']['assetDet'].get('cphNumber','') != '':
                        if x['data']['assetDet'].get('cphNumber','') not in newArray:
                            newArray.append(x['data']['assetDet'].get('cphNumber',''))
        return Response(newArray)

I have tried to get access by

usernames = self.request.user

But I am getting an error as

'BasePermissionMetaclass' object is not iterable

I am not able to figure it out how to get the field? Any suggestions will be of great help! Thanks!

Upvotes: 1

Views: 227

Answers (1)

JPG
JPG

Reputation: 88499

It seems like a typo.

Change

permission_classes = (permissions.IsAuthenticated)

to

permission_classes = (permissions.IsAuthenticated,) # put a comma
                                               ^^^^^

Upvotes: 1

Related Questions