Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 何时使用;AbstractBaseUser“;在Django?_Python_Django_Django Models - Fatal编程技术网

Python 何时使用;AbstractBaseUser“;在Django?

Python 何时使用;AbstractBaseUser“;在Django?,python,django,django-models,Python,Django,Django Models,当我可以给User模型指定任何字段时,这两者之间有什么区别,以及AbstractBaseUser的目的是什么?哪一个更适合与python social auth一起使用 class User(models.Model): username = models.CharField(max_length=50) is_active = models.BooleanField(default=True) full_name = models.CharField(max_lengt

当我可以给
User
模型指定任何字段时,这两者之间有什么区别,以及
AbstractBaseUser
的目的是什么?哪一个更适合与python social auth一起使用

class User(models.Model):
    username = models.CharField(max_length=50)
    is_active = models.BooleanField(default=True)
    full_name = models.CharField(max_length=100)
    date_of_birth = models.DateField()


AbstractUser
User
模型进行子类化。因此,单个模型将具有本机
用户
字段,以及您定义的字段

其中,将字段分配给相等的
用户
,即
用户=模型。ForeignKey(用户)
正在创建从一个模型到
用户
模型的联接

您可以使用其中任何一种,但django推荐的方法是创建联接,因此使用=>
user=models.ForeignKey(user)

子类的正确模型是
django.contrib.auth.models.AbstractUser
,如文档中所述:


我想你的意思是
AbstractUser
而不是
AbstractBaseUser
。但我明白了。谢谢。@norbertpy我添加了一个带有文档链接的编辑。感谢您指出这一点。了解
AbstractUser
AbstractBaseUser
之间的区别非常重要,如中所述。
class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=50)
    is_active = models.BooleanField(default=True)
    full_name = models.CharField(max_length=100)
    date_of_birth = models.DateField()