django 扩展user字段

本文介绍了两种在Django中扩展用户模型的方法:一是直接替换默认的User模型,创建自定义的MyUser模型,包括额外的date_of_birth和device_id字段,并在admin.py中定制表单样式;二是通过建立一对一关系的新模型UserProfile来扩展用户信息,包含description和scope字段。同时,文章还展示了相关代码实现。
摘要由CSDN通过智能技术生成

方式一:用自定义的user对象替换django中的默认

model.py中自定义user对象
'''
自定义用户管理
'''
class
MyUserManager(BaseUserManager):

    def create_user(self, email, date_of_birth, device_id,password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        
if not email:
            raise ValueError('Users must have an email address')
        user = self.model(
            email=self.normalize_email(email),
            date_of_birth=date_of_birth,
            device_id=device_id,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, date_of_birth, password,device_id):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """

        
user = self.create_user(
            email,
            password=password,
            date_of_birth=date_of_birth,
            device_id=device_id,
        )
        user.is_admin = True
        
user.save(using=self._db)
        return user

"""
自定义用户
"""
class
MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值