'NoneType' object has no attribute 'save' when saving a new user in a Django with a custom user model

When I try to register a user, adapting what I learnt from Building a Custom User Model with Extended Fields youtube tutorial, I get an AttributeError: 'NoneType' object has no attribute 'save':

views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import get_user_model
from django.conf import settings
User = settings.AUTH_USER_MODEL
from django.db import IntegrityError
from django.contrib.auth import login, logout, authenticate
from .forms import TodoForm
from .models import Todo
from django.utils import timezone
from django.contrib.auth.decorators import login_required

def home(request):
    return render(request, 'todo/home.html')

def signupuser(request):
    if request.method == 'GET':
        return render(request, 'todo/signupuser.html', {'form':UserCreationForm()})
    else:
        if request.POST['password1'] == request.POST['password2']:
            try:
                db = get_user_model()
                user = db.objects.create_user(request.POST['email'], request.POST['username'], 
                                            request.POST['firstname'], request.POST['company'], request.POST['mobile_number'], 
                                            password=request.POST['password1'])
                user.save()

The user is still registered but I can't login with the profvided password ... What could be the reason ?

Here is the full error:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/signup/

Django Version: 4.1.5
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'todo']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/Users/remplacement/Documents/Work/4eme/django3-todowoo-project-master/tod_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Users/remplacement/Documents/Work/4eme/django3-todowoo-project-master/tod_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/remplacement/Documents/Work/4eme/django3-todowoo-project/todo/views.py", line 26, in signupuser
    user.save()

Exception Type: AttributeError at /signup/
Exception Value: 'NoneType' object has no attribute 'save'

Here is my custom user model:

models.py:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager


class CustomAccountManager(BaseUserManager):

    def create_superuser(self, email, user_name, first_name, password, **other_fields):

        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)

        if other_fields.get('is_staff') is not True:
            raise ValueError(
                'Superuser must be assigned to is_staff=True.')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.')

        return self.create_user(email, user_name, first_name, password, **other_fields)

    def create_user(self, email, user_name, first_name,  company, mobile_number, password, **other_fields):

        if not email:
            raise ValueError(('You must provide an email address'))

        email = self.normalize_email(email)
        user = self.model(email=email, user_name=user_name, first_name=first_name, 
                        company=company, mobile_number=mobile_number, password=password, **other_fields)
        user.set_password(password)
        user.save()
        return 

class Newuser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(('email address'), unique=True)
    user_name = models.CharField(max_length=150, unique=True)
    first_name = models.CharField(max_length=150, blank=True)
    mobile_number = models.CharField(max_length=10)
    company = models.CharField(max_length=5)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['user_name', 'first_name', 'mobile_number']

    def __str__(self):
        return self.user_name

settings.py

AUTH_USER_MODEL = 'todo.NewUser'
Back to Top