python - 类型错误 : create_superuser() missing 1 required positional argument: 'profile_picture'

标签 python python-3.x django django-custom-user django-custom-manager

添加 profile_picture 字段后出现以下错误:

TypeError: create_superuser() missing 1 required positional argument: 'profile_picture'

此 profile_picture 字段是一个设置为“Null = True”的“ImageField”。

我尝试了以下方法:def create_user(...., profile_picture=None, ....)。它没有用。

错误仅在我从那里创建 super 用户时出现在命令提示符下。

这是我的模型.py

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


class UserManager(BaseUserManager):
    def create_user(self, email, full_name, profile_picture=None, gender=None, password=None, is_admin=False, is_staff=False, is_active=True):
        if not email:
            raise ValueError("User must have an email")
        if not password:
            raise ValueError("User must have a password")
        if not full_name:
            raise ValueError("User must have a full name")

        user = self.model(
            email=self.normalize_email(email)
        )
        user.full_name = full_name
        user.set_password(password)  # change password to hash
        # user.profile_picture = profile_picture
        user.gender = gender
        user.admin = is_admin
        user.profile_picture = profile_picture
        user.staff = is_staff
        user.active = is_active
        user.save(using=self._db)
        return user

    def create_staffuser(self, email, profile_picture, gender, full_name, password=None):
        user = self.create_user(
            email,
            full_name,
            profile_picture,
            gender,
            password=password,
            is_staff=True,
        )
        return user

    def create_superuser(self, email, profile_picture, gender, full_name, password=None):
        user = self.create_user(
            email,
            full_name,
            profile_picture,
            gender,
            password=password,
            is_staff=True,
            is_admin=True,
        )
        return user


class User(AbstractBaseUser):
    username = models.CharField(max_length=255)
    full_name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255, unique=True,)
    profile_picture = models.ImageField(upload_to='user_data/profile_picture', null=True, blank=True)
    gender = models.CharField(max_length=255, blank=True, default='rather_not_say')
    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)  # a admin user; non super-user
    admin = models.BooleanField(default=False)  # a superuser
    # notice the absence of a "Password field", that's built in.

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['full_name', 'gender']  # Email & Password are required by default.

    objects = UserManager()

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
         # The user is identified by their email address
         return self.email

    def __str__(self):              # __unicode__ on Python 2
         return self.email

    @staticmethod
    def has_perm(perm, obj=None):
         # "Does the user have a specific permission?"
         # Simplest possible answer: Yes, always
        return True

    @staticmethod
    def has_module_perms(app_label):
         # "Does the user have permissions to view the app `app_label`?"
         # Simplest possible answer: Yes, always
         return True

    @property
    def is_staff(self):
         # "Is the user a member of staff?"
         return self.staff

    @property
    def is_admin(self):
         # "Is the user a admin member?"
         return self.admin

    @property
    def is_active(self):
         # "Is the user active?"
         return self.active

最佳答案

您可以将 username 添加到 REQUIRED_FIELDS。之后 python manage.py createsuperuser 请求用户名字段并且它起作用了。

REQUIRED_FIELDS = ['full_name', 'gender', 'username',]

关于python - 类型错误 : create_superuser() missing 1 required positional argument: 'profile_picture' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49474632/

相关文章:

python - 为什么 py.test 向后运行模块 doctests?

python - 在 Django 1.7 上使用表单和 View 更新模型字段

python - 使用 2 位数字与 3 位数字时,django 中的数学结果不正确?

python - 为什么会出现▯这个字符?

python - 运行 python3 -bb 时,为什么 set([b'foo', u'foo']) 失败但 set([b'foo', u'bar']) 成功?

javascript - Angular ui bootstrap modal 传递多个参数

c# - microsoft.scripting.hosting.scriptscope 不包含 'SomeFunction' 的定义

python - 我想更新字典。但它生成,RuntimeError : dictionary changed size during iteration

python - pandas 面板数据的百分比变化

python - 使用 boto3 从 S3 存储桶中读取文件内容