Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 282
Sam R. Avatar asked Sep 14 '25 04:09

Sam R.


1 Answers

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 i.e. 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

like image 180
Aaron Lelevier Avatar answered Sep 16 '25 18:09

Aaron Lelevier