I am trying to implement user registration using djoser. I believe I am passing in all the correct data in the body but I return a 500 error: "AttributeError: 'NoneType' object has no attribute 'is_active'"
I have tried overwriting list_display and list_filter in BaseUserAdmin. I also have tried moving the is_active attribute to different areas of the model.
models.py
from django.contrib.auth.models import AbstractUser, AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
# managing the creation and user and superuser
class UserAccountManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
# first_name, last_name,
if not email:
raise ValueError('Users must provide an email address.')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_active') is not True:
raise ValueError('Superuser must have is_active=True.')
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, password, **extra_fields)
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
objects = UserAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
serializers.py
from djoser.serializers import UserCreateSerializer
from django.contrib.auth import get_user_model
User = get_user_model()
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model = User
fields = ('id', 'email', 'password')
Traceback
Traceback (most recent call last):
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/djoser/views.py", line 132, in perform_create
user = serializer.save(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/rest_framework/serializers.py", line 212, in save
self.instance = self.create(validated_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/djoser/serializers.py", line 40, in create
user = self.perform_create(validated_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/law/.local/share/virtualenvs/backend-2t9m5bj9/lib/python3.11/site-packages/djoser/serializers.py", line 50, in perform_create
user.is_active = False
^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'is_active'
[31/Oct/2023 20:55:55] "POST /auth/users/ HTTP/1.1" 500 111290
You don't return the created user: