简体   繁体   中英

When to use “AbstractBaseUser” in Django?

What is the difference between these two and what is the purpose of AbstractBaseUser when I can give any field to User model? Which one is better to use with 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()

and

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()

AbstractUser subclasses the User model. So that single model will have the native User fields, plus the fields that you define.

Where as assigning a field to equal User ie user=models.ForeignKey(User) is creating a join from one model to the User model.

You can use either one, but the recommended way for django is to create a join, so using => user=models.ForeignKey(User)

The correct model to sublclass is django.contrib.auth.models.AbstractUser as noted here in the docs:

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#extending-django-s-default-user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM