4

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()
Alasdair
  • 298,606
  • 55
  • 578
  • 516
Sam R.
  • 16,027
  • 12
  • 69
  • 122

1 Answers1

4

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

Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • 1
    I think you mean `AbstractUser` instead of `AbstractBaseUser`. But I got the point. Thank you. – Sam R. Nov 20 '14 at 21:55
  • @norbertpy I added an edit with the link to the docs. Thank you for pointing that out. – Aaron Lelevier Nov 21 '14 at 01:37
  • Is important here to understand the difference between `AbstractUser` and `AbstractBaseUser` as its explained in [this answer](http://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django#21514469). – Francisco Puga Mar 03 '17 at 12:39