Search code examples
pythondjangousergroupsdjango-2.2

TypeError: create_user() missing 1 required positional argument: 'group'


Everything worked fine, until I dropped the database and run the migrations again. Now, I'm not able to create superuser again.

The error I got is:

TypeError: create_user() missing 1 required positional argument: 'group'

I create superuser from: python3 manage.py createsuperuser from the terminal.

Anyone faced the same problem and is there any solutions to fix the error?

models.py

class CustomUserManager(BaseUserManager):

    def create_user(self, email: str, password: str, group: Group, **extra_fields):
        if not email:
            raise ValueError(_('The Email must be set'))

        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()

        if group is not None:
            group.user_set.add(user)

        return user
    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('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 CustomUser(AbstractUser):

    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email

    username                        = models.CharField(max_length=30, blank=True, default='')
    is_superuser                    = models.BooleanField(default=True)
    is_admin                        = models.BooleanField(default=True)
    is_employee                     = models.BooleanField(default=True)
    is_headofdepartment             = models.BooleanField(default=True)
    is_reception                    = models.BooleanField(default=True)
    is_patient                      = models.BooleanField(default=True)
    is_active                       = models.BooleanField(default=True)
    is_staff                        = models.BooleanField(default=True)

forms.py

class UserForm(ModelForm):
    
    group = forms.ModelChoiceField(queryset=Group.objects.all())
    #temp['group']=request.POST.get('group')
    #role = forms.ChoiceField(choices=ROLE_CHOICES, label='', widget=forms.RadioSelect(attrs={}))


    class Meta:
        model = CustomUser
        fields = [
            'email',
            'password',
            'group',
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['group'] = ModelChoiceField(
            queryset=Group.objects.all(),
            empty_label='No group'
        )

Solution

  • The create_superuser management command uses the orignal User.objects.create_user function.

    You added a positional argument which is required. The create_superuser command does not put any group argument when calling it.

    You should make this group argument optional if you really need it.

    from typing import Optional
    
    class CustomUserManager(BaseUserManager):
    
        def create_user(self,
                        email: str,
                        password: str,
                        group: Optional[Group] = None,
                        **extra_fields):
            if not email:
                raise ValueError(_('The Email must be set'))
    
            email = self.normalize_email(email)
            user = self.model(email=email, **extra_fields)
            user.set_password(password)
            user.save()
    
            if group is not None:
                group.user_set.add(user)
    
            return user