Python Forum

Full Version: Django __init__() got an unexpected keyword argument 'any'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I do not understand what happened to my application, I was trying to obtain the detail of a user with email field, now I can't list the users that I have registered in the database, it is giving me this error:


I commented all the code and let the get function to list the users but it doesn't work

Quote:TypeError at /users/
__init__() got an unexpected keyword argument

view.py
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from src.apps.account.models import CustomUser
from src.apps.account.serializer import CustomUserSerializer


class UserList(APIView):
    def get(self, request, format=None, users=None):
        users = CustomUser.objects.all()
        serialized_users = CustomUserSerializer(users, any=True)
        return Response(serialized_users.data, status=status.HTTP_200_OK)


# class UserRegister(APIView):
#     def post(self, request):
#         # user_info = request.data
#         serialized_user = CustomUserSerializer(data=request.data)
#         if serialized_user.is_valid():
#             new_user = serialized_user.save()
#             new_user.set_password(new_user.password)
#             new_user.save()
#             return Response(new_user, status=status.HTTP_201_CREATED)
#         return Response(serialized_user.errors)

# class UserDetail(APIView):
#     def get(self, request, pk):
#         user_detail = CustomUser.objects.get(email=pk)
#         serialized_detail = CustomUserSerializer(user_detail)
#         return Response(serialized_detail.data)
url.py
from django.urls import path
from . import views
from src.apps.account.views import UserList

urlpatterns = [
    path("users/", UserList.as_view(), name="user-list"),
    # path("users/register/", UserRegister.as_view(), name="user-new"),
    # path("users/detail/<str:email>", UserDetail.as_view(), name="user-detail")
]
CustomUserSerializer
from rest_framework import serializers
from rest_framework import status
from rest_framework.response import Response
from .models import CustomUser


class CustomUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomUser
        fields = "__all__"
        extra_kwargs = {"password": {"write_only": True}}
why am i getting this error?
You probably meant to pass 'many' and not 'any' to the serializer.

class UserList(APIView):
    def get(self, request, format=None, users=None):
        users = CustomUser.objects.all()
        serialized_users = CustomUserSerializer(users, many=True)
        return Response(serialized_users.data, status=status.HTTP_200_OK)
Thats the problem!! typed error, thank you!!!!