pythondjangodjango-rest-frameworkdjango-registration

TypeError at /api/register/ 'module' object is not callable


I am trying to register users using django rest framework but this is the error i am getting, Please help Identify the issue

TypeError at /api/register/ 'module' object is not callable Request Method: POST Request URL: http://127.0.0.1:8000/api/register/ Django Version: 3.1.5 Exception Type: TypeError Exception Value:
'module' object is not callable Exception Location: C:\Users\ben\PycharmProjects\buddyroo\lib\site-packages\rest_framework\generics.py, line 110, in get_serializer Python Executable: C:\Users\ben\PycharmProjects\buddyroo\Scripts\python.exe Python Version: 3.8.5

below is RegisterSerializer

from django.contrib.auth.password_validation import validate_password
from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.validators import UniqueValidator


class RegisterSerializer(serializers.ModelSerializer):
    email = serializers.EmailField(
        required=True,
        validators=[UniqueValidator(queryset=User.objects.all())]
    )

    password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
    password2 = serializers.CharField(write_only=True, required=True)

    class Meta:
        model = User
        fields = ('username', 'password', 'password2', 'email', 'first_name', 'last_name')
        extra_kwargs = {
            'first_name': {'required': True},
            'last_name': {'required': True}
        }

    def validate(self, attrs):
        if attrs['password'] != attrs['password2']:
            raise serializers.ValidationError({"password": "Password fields didn't match."})

        return attrs

    def create(self, validated_data):
        user = User.objects.create(
            username=validated_data['username'],
            email=validated_data['email'],
            first_name=validated_data['first_name'],
            last_name=validated_data['last_name']
        )

        user.set_password(validated_data['password'])
        user.save()

        return user

and RegisterView.py

from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated, AllowAny  # <-- Here
from rest_framework.response import Response
from rest_framework.views import APIView

from api import UsersSerializer, RegisterSerializer


class RegisterView(generics.CreateAPIView):
    queryset = User.objects.all()
    serializer_class = RegisterSerializer
    permission_classes = (AllowAny,)

Solution

  • I suppose that the name of the module (file) where RegisterSerializer is defined is RegisterSerializer.py.

    If this is the case, in the RegisterView.py you are importing the module RegisterSerializer and not the class.

    So, it should be

    from api.RegisterSerializer import RegisterSerializer
    

    In Python it is common to have more than one class in one module, so I would advise you to rename your modules to serializers.py and views.py and put all your serializers and views there.

    Of course, if they are many, you may split this and create serializers/views packages and put several serializers/views modules there: user_serializers.py, whaterver_serializers.py...