Home > Net >  How to resolve the error: TypeError at /signup save() missing 1 required positional argument: '
How to resolve the error: TypeError at /signup save() missing 1 required positional argument: '

Time:12-01

I'm trying to register users using AbstractUser but I'm getting an error. Although I'm getting error, users still get registered but I can't handle login with the registered users.

The error goes thus: The error goes thus:

The post request

My views.py goes thus:

def signup(request):
    if request.method == 'POST':
        first_name = request.POST['first-name']
        last_name = request.POST['last-name']
        username = request.POST['username']
        email = request.POST['email']
        phone_no = request.POST['phone-no']
        password = request.POST['password']
        password2 = request.POST['password2'] 
        
        if password==password2:
            if CustomUser.objects.filter(username=username).exists():
                messages.info(request, 'Username Taken')
                return redirect('signup')
            elif CustomUser.objects.filter(email=email).exists():
                messages.info(request, 'Email Already Exist')
                return redirect('signup')
            else:
                CustomUser.objects.create_user(first_name=first_name, last_name=last_name, username=username, email=email, phone_no=phone_no, password=password)
                CustomUser.save(commit=False)
                return redirect('login')
        else:
            messages.info(request, 'Passwords Not Matching')
            return redirect('signup')
    else:      
        return render(request, 'signup.html')

And my models.py:

class CustomUser(AbstractUser):
    id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    username = models.CharField(max_length=100, unique=True)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=150)
    phone_no = models.CharField(max_length=20)
    is_end_user = models.BooleanField(default=True)
    is_smart_earner = models.BooleanField(default=False)
    is_top_user = models.BooleanField(default=False)

CodePudding user response:

Remove the CustomUser.save(commit=False). It makes no sense, the record is created with the .create_user(…) method [Django-doc].

You can only call the .save(…) method [Django-doc] on model objects, since these represent a record in the corresponding table. Using it on the CustomUser class makes not much sense, since it represents a table, and table itself is not altered when creating a record.

Furthermore your .save() call uses a parameter commit=…, but that does not make much sense either: the .save(…) of a ModelForm can accept a commit=… parameter, not the one for a Model.

I would however advise to work with a ModelForm. Such ModelForm can automatically validate unique fields, and save the created user. Django already has a basic ModelForm for this: the UserCreationForm [Django-doc]. With some small changes, it can likely work for your custom user model.

  • Related