0

I am using mongoengine with Django rest framework as my backend and simple jwt to authenticate the user. I am able to register/login the user successfully but when I use permission_classes of rest framework (IsAuthenticated), the api gives me the error like - ValueError: Field 'id' expected a number but got '64bfbbcf2ec0b5d0279a77d4'.

I know that it is using mongo's unique object id as the user id which is creating problem but I am not able to avoid it, is there any better way to handle it?

I have tried below part -

INSTALLED_APPS = [
    # Other installed apps
     'rest_framework',
     'rest_framework_simplejwt',
     'myapp',
]

AUTH_USER_MODEL = 'myapp.CustomUser'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

SIMPLE_JWT = {
    'AUTH_HEADER_TYPES': ('Bearer',),
}

Is there any other modification that I need to do?

I have tried one more case where I am explicitly creating a user_id by using django (Object Documment Mapper)ODM but this is also not working

class GetUsersAPI(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        return Response(status=status.HTTP_200_OK)
3
  • If you are in a development environment, try to delete all project migrations and run python manage.py makemigrations and python manage.py migrate as suggested in the SO answer of this link. Commented Jul 25, 2023 at 18:49
  • 1
    @claudius I think makemigrations and migrate will not work with mongo DB as it is non -relational DB. It will give database improperly configured error. Commented Jul 26, 2023 at 6:33
  • Could you share one of your JWT tokens for inspection? Commented Jul 26, 2023 at 14:03

0

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.