0

模型.py

User = get_user_model()
class CustomUser(AbstractUser):
   sign = models.ImageField(upload_to=image_saving)
   objects = CustomUserManager()

这是我的models.py 我试图向django User 模型添加一个额外的字段。

设置.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'django_restful_admin',
'django_jalali',

'm',
'users',
]

AUTH_USER_MODEL = 'users.CustomUser'

我已添加AUTH_USER_MODEL到我的 setting.py 中。这是我安装的应用程序。

管理者.py

class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifiers
for authentication instead of usernames.
"""
def create_user(self, email, password, **extra_fields):
    """
    Create and save a User with the given email and password.
    """
    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()
    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)

这是我的managers.py ...我想在sign我的用户模型中添加一列。

4

1 回答 1

0

我很简单地解决了它。我只是继承了 django 用户模型并添加了我的额外字段。:)

于 2021-08-02T05:25:31.737 回答