[Answered ]-Overriding Google-Auth user creation

1👍

Create the Custom User Model in models.py

You can use the Django example, or follow with ours below. We will simplify the process here.

accounts/models.py

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a admin user; non super-user
    admin = models.BooleanField(default=False) # a superuser

    # notice the absence of a "Password field", that is built in.

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = [] # Email & Password are required by default.

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin

Update settings module (aka settings.py):

1. Run Migrations

python manage.py makemigrations
python manage.py migrate
  1. Update settings.py
    AUTH_USER_MODEL = 'accounts.User'
  1. Run Migrations again
    python manage.py makemigrations
    python manage.py migrate
  1. Create a superuser
    python manage.py createsuperuser
  1. Some practical thoughts

Setting the AUTH_USER_MODEL means that we can use many third party packages that leverage the Django user model; packages like Django Rest Framework, Django AllAuth, Python Social Auth, come to mind.

Now, every time you need the user model, use:

    from django.contrib.auth import get_user_model
    User = get_user_model()

This provides other developers and our future selves the kind of peace of mind that only well-thought-out code can provide; get_user_model is one of those pieces the core Django developers decided on long ago.

6 But what about User foreign keys? At some point you’ll be faced with this situation:

    class SomeModel(models.Model):
        user = models.ForeignKey(...)

So what to use? get_user_model accounts.models.User or what? Actually, you’ll use settings.AUTH_USER_MODEL every time regardless of user model customization. So it will look like this:

    from django.conf import settings
    
    User = settings.AUTH_USER_MODEL
    
    class SomeModel(models.Model):
        user = models.ForeignKey(User, on_delete=models.CASCADE)

hope this answers your question… ?

Leave a comment