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:UsersbenPycharmProjectsbuddyroolibsite-packagesrest_frameworkgenerics.py, line 110, in get_serializer
Python Executable: C:UsersbenPycharmProjectsbuddyrooScriptspython.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,)
Source: Python Questions