Skip to main content
 首页 » 编程设计

python之在我的模型中继承django的默认用户模型

2024年10月25日29jyk

我有一个关于 django 的用户继承的问题。

如果我想通过电子邮件和密码注册用户,我已经浏览了 django 的文档,在那里我看到了用户模型的继承。

我的问题:

我将创建的默认用户模型和自定义用户模型有什么区别? 我可以在自定义用户模型中使用权限并添加组吗?

如果我想在我的模型中继承 django 的默认用户怎么办?

class Person(User): 
    address = models.CharField(max_length=100) 
    def __unicode__(self): 
        return self.address 

这里的用户模型是django默认的用户模型? 我可以这样继承吗?

请您参考如下方法:

基于 django specifying a custom user model

The easiest way to construct a compliant custom User model is to inherit from AbstractBaseUser. AbstractBaseUser provides the core implementation of a User model, including hashed passwords and tokenized password resets.

另外 Extending the existing User model可能是一个不错的选择。