从自定义模型创建用户时出错

Error when creating user from custom model

我对 Django 比较陌生,但刚刚创建完自定义用户模型而不是使用基础。当 运行 createsuperuser 我收到以下错误堆栈时:

File "C:\Users\adamt\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\base_user.py", line 66, in save super().save(*args, **kwargs) TypeError: save() got an unexpected keyword argument 'user'

我的自定义模型,create_usercreate_superuser 是:

    def create_user(self, email, username, FirstName, Surname, password=None):
        if not email:
            raise ValueError("Users must have an Email Address")
        if not username:
            raise ValueError("Users must have a Username")
        if not FirstName:
            raise ValueError("Users must have a First Name")
        if not Surname:
            raise ValueError("Users must have a Surname")
        
        user = self.model(
            email=self.normalize_email(email),
            username=username,
            FirstName=FirstName,
            Surname=Surname,
            )
        
        user.set_password(password)
        user.save(user=self._db)
        return user
        
    def create_superuser(self, email, username, FirstName, Surname, password):
        user = self.create_user(
        email=self.normalize_email(email),
        username=username,
        FirstName=FirstName,
        Surname=Surname,
        password=password,
        )
            
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        
        user.save(user=self._db)
        return user
        
            
        
class Account(AbstractBaseUser):
    email = models.EmailField(verbose_name ="email", max_length=60, unique=True)
    username = models.CharField(max_length=100, unique=True)
    date_joined = models.DateTimeField(verbose_name="date joined", auto_now_add=True)
    last_login = models.DateTimeField(verbose_name="last login", auto_now=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    DateofBirth = models.DateField(verbose_name="Date of Birth")
    MarketingOptIn = models.BooleanField(verbose_name="Marketing Opt In", default=False)
    FirstName = models.CharField(verbose_name="First Name", max_length=100)
    Surname = models.CharField(verbose_name="Surname", max_length=100)
    
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username','FirstName','Surname', ]
    
    objects = AccountManager()
    
    def __str__(self):
        return self.username
    
    def has_perm(self, perm, obj=None):
        return self.is_admin
    
    def has_module_perms(self, app_label):
        return True

我显然错过了一些东西,但我这辈子都看不到了。任何帮助都会很棒。

当你.save(…) [Django-doc]一个Model对象时,你可以使用一个using=…参数来指定数据库,而不是user=…, 所以:

    def create_user(self, email, username, FirstName, Surname, password=None):
        # …
        user.save(<b>using=self._db</b>)
        return user
        
    def create_superuser(self, email, username, FirstName, Surname, password):
        # …
        
        user.save(<b>using=self._db</b>)
        return user