如何将 class 个人资料中的 phone 数字字段添加到 class 用户?

how can i add phone number field from class profile to class user?

我需要将 phone 数字添加到我所需的模型字段中,所以我正在这样做但无法正常工作 所以如果大家知道我怎么加一个Phone号,请帮帮我:

#models.py

    from django.db import models
        from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
        from phonenumber_field.modelfields import PhoneNumberField
        
        
        class UserManager(BaseUserManager):
            def create_user(self, phone_number, email, password=None, is_active=True, is_staff=False, is_admin=False):
                if not email:
                    raise ValueError("User Must have an Email Address")
                if not password:
                    raise ValueError("User Must have a Password")
                if not phone_number:
                    raise ValueError("User Must have a Phone Number")
                user_obj = self.model(email=self.normalize_email(email))
                user_obj.set_password(password)  # change user password
                user_obj.phone_number(phonenumber_field)
                user_obj.staff = is_staff
                user_obj.active = is_active
                user_obj.admin = is_admin
                user_obj.save(using=self._db)
                return user_obj
        
            # staff user
            def create_staffuser(self, email, password=None):
                user = self.create_user(
                    email,
                    password=password,
                    phone_number=PhoneNumberField,
                    is_staff=True
                )
                return user
            # admin user
        
            def create_superuser(self, email, password=None):
                user = self.create_user(
                    email,
                    password=password,
                    phone_number=PhoneNumberField,
                    is_staff=True,
                    is_admin=True
                )
                return user
        
        
        class User(AbstractBaseUser):
            email = models.EmailField(max_length=255, unique=True)
            # full_name = models.CharField(max_length=255, blank=True, null=True)
            active = models.BooleanField(default=True)  # can log in
            staff = models.BooleanField(default=False)  # staff user non super user
            admin = models.BooleanField(default=False)  # super user
            timestamp = models.DateTimeField(auto_now_add=True)
            phone_number = PhoneNumberField()
        
            USERNAME_FIELD = 'email'  # username
            REQUIRED_FIELDS = []  # FULL_NAME
            object = UserManager()
        
            def __str__(self):
                return self.email
        
            def get_full_name(self):
                return self.email
        
            def get_short_name(self):
                return self.email
        
            def has_perm(self, perm, obj=None):
                return True
        
            def has_module_perms(self, app_label):
                return True
        
            @property
            def is_staff(self):
                return self.staff
        
            @property
            def is_admin(self):
                return self.admin
        
            @property
            def is_active(self):
                return self.active
        
        
        class Profile(models.Model):
            user = models.OneToOneField(User, on_delete=models.CASCADE)

我想在 class 个人资料中添加 phone 号码,并将其添加到用户 class 的必填字段中,但我不能

每次我有错误:raise TypeError("Can't convert %s to PhoneNumber." % type(value).name) TypeError: 无法将 int 转换为 PhoneNumber.

您不能将 ModelField class 添加到属性。而是尝试通过方法参数传递值。根据 documentation,如果该字段是必需的,您应该能够传递该值。

因此,像这样更新您的管理器方法:

class UserManager(BaseUserManager):
    def create_user(self, email, phone_number, password=None, is_active=True, is_staff=False, is_admin=False):
        if not email:
            raise ValueError("User Must have an Email Address")
        if not password:
            raise ValueError("User Must have a Password")
        if not phone_number:
            raise ValueError("User Must have a Phone Number")
        user_obj = self.model(email=self.normalize_email(email))
        user_obj.set_password(password)  # change user password
        user_obj.phone_number = phone_number
        user_obj.staff = is_staff
        user_obj.active = is_active
        user_obj.admin = is_admin
        user_obj.save(using=self._db)
        return user_obj

    # staff user
    def create_staffuser(self, email, phone_number, password=None):
        user = self.create_user(
            email,
            password=password,
            phone_number=phone_number,
            is_staff=True
        )
        return user
    # admin user

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

并将phone_number添加到模型中的必填字段:

REQUIRED_FIELDS = ['phone_number']