Home > Mobile >  CustomAccounManager.create_user() missing 1 required positional argument: 'email'
CustomAccounManager.create_user() missing 1 required positional argument: 'email'

Time:03-26

I am trying creating a custom user model in django - which I am using as a backend. On django admin I can create users without issue. My problem arises when I try to register a new account via postman.

In postman I have the email field filled out in the body, yet I still get the error missing 1 required positional argument: 'email'.

models.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.utils.translation import gettext_lazy as _



class CustomAccounManager(BaseUserManager):

    def create_superuser(self, email, username, password, **other_fields):

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

        return self.create_user(self, email, username, password, **other_fields)

    def create_user(self, email, username, password, **other_fields):
    
        if not email:
            raise ValueError(_('You must provide an email address'))    
    
        email = self.normalize_email(email)
        user = self.model(email=email, username=username, **other_fields)
        user.set_password(password)
        user.save()
        return user

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    username = models.CharField(max_length=150, unique = True) 
    is_student = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser =  models.BooleanField(default=False) 
    is_active = models.BooleanField(default=True) 

    objects = CustomAccounManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return self.username

serializers.py

from rest_framework import serializers
from rest_framework.authtoken.models import Token


from .models import User


class UserSerializer(serializers.ModelSerializer):
    

    class Meta:
        model = User
        fields = ('id', 'username', 'password')
        extra_kwargs = {'password': {'write_only': True, 'required': False}}

    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        Token.objects.create(user=user)
        return user

views.py

from rest_framework import viewsets
from django.shortcuts import render
from .models import User
from .serializers import UserSerializer


class UserViewSet(viewsets.ModelViewSet):
    queryset =User.objects.all()
    serializer_class = UserSerializer  


Any help is appreciated!

CodePudding user response:

This is because you are making USERNAME_FIELD = 'email' in your custom user model, so you have to set USERNAME_FIELD = 'username' and in the email field, set

email = models.EmailField(_('email address'), null=True, blank=True)

but if you need the USERNAME_FIELD = 'email' you have to send it with other data, it's based on your case.

  • Related