django 类型错误:create_superuser()缺少1个必需的位置参数:'配置文件图片'

dffbzjpn  于 2022-12-01  发布在  Go
关注(0)|答案(8)|浏览(122)

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

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

该profile_picture字段是设置为“空=真”的“图像字段”。
我已尝试以下方法:def create_user(...., profile_picture=None, ....)。它没有工作。
当我从命令提示符创建超级用户时,该错误仅出现在命令提示符中。
这里是我的models.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
4szc88ey

4szc88ey1#

你可以把username添加到REQUIRED_FIELDS中,然后python manage.py createsuperuser要求输入用户名字段,它就可以工作了。

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

f1tvaqid2#

您还需要创建create_superuser函数:

class UserManager(BaseUserManager):
    def create_user(self, email, full_name, profile_picture, 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.admin = is_admin
        user.staff = is_staff
        user.active = is_active
        user.save(using=self._db)
        return user
        
    def create_superuser(self, email, full_name, profile_picture, password=None, **extra_fields):
        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)
        user.profile_picture = profile_picture
        user.admin = True
        user.staff = True
        user.active = True
        user.save(using=self._db)
        return user

祝您好运!

m528fe3b

m528fe3b3#

我也遇到了同样的问题,结果发现在REQUIRED_FIELDS列表中有一个错误的名字。这个列表告诉django框架在创建过程中也要询问名字。因为它没有询问,而你却让它成为了必要。我希望它能有所帮助,祝你好运

cwtwac6a

cwtwac6a4#

我通过一些改动解决了这个问题
我的旧代码

class User(AbstractUser):
    username = None
    name = models.CharField(max_length=200, null=True)
    email = models.EmailField(unique=True, null=True)
    bio = models.TextField(null=True)

    avatar = models.ImageField(null=True, default="avatar.svg")
 
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

并且我将用户名字段编辑为电子邮件字段

class User(AbstractUser):
    username = models.EmailField(unique=True, null=True)
    name = models.CharField(max_length=200, null=True)
    bio = models.TextField(null=True)

    avatar = models.ImageField(null=True, default="avatar.svg")
 
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

计算username = None => username = models.EmailField(unique=True, null=True)
快乐编码:)

ds97pgxw

ds97pgxw5#

最好在www.example.com的迁移文件中创建一个类0001.initial.py。定义一个类,其中包含登录所需的所有字段,并提供空的依赖项和操作块。就是这样

hvvq6cgz

hvvq6cgz6#

在migrations文件夹中创建一个0001_initial.py文件,然后执行下面的代码,它将工作...

from django.db import migrations
from api.user.models import CustomUser

class Migration(migrations.Migration):
    
    def seed_data(apps, schema_editor):
        user = CustomUser(name='name',
                          email='mail@gmail.com',
                          is_staff=True,
                          is_superuser=True,
                          phone='987654321',
                          gender='Male'
        
                        )
        user.set_password('anypassword')
        user.save()
    
    

    dependencies=[

    ]

    operations=[
            migrations.RunPython(seed_data),
        ]
laximzn5

laximzn57#

您需要将**“profile_picture”添加到”www.example.com““User(AbstractBaseUser)"的**“REQUIRED_FIELDS”models.py 中:

# "models.py"
class User(AbstractBaseUser):
    # ...                           # Here
    REQUIRED_FIELDS = ['full_name', 'profile_picture', 'gender']
    # ...
r8uurelv

r8uurelv8#

您需要将profile_picture添加到models.pyUser(AbstractBaseUser)类的REQUIRED_FIELDS中:

相关问题