Reading-Notes


Project maintained by eslamakram Hosted on GitHub Pages — Theme by mattgraham

Django Custom User

AbstractUser vs AbstractBaseUser There are two modern ways to create a custom user model in Django: AbstractUser and AbstractBaseUser. In both cases we can subclass them to extend existing functionality however AbstractBaseUser requires much, much more work. Seriously, don’t mess with it unless you really know what you’re doing. And if you did, you wouldn’t be reading this tutorial, would you?

So we’ll use AbstractUser which actually subclasses AbstractBaseUser but provides more default configuration.

Custom User Model Creating our initial custom user model requires four steps:

  1. update config/settings.py
  2. create a new CustomUser model
  3. create new UserCreation and UserChangeForm
  4. update the admin

By default, the User model in Django uses a username for authentication. Instead if you wish to use email, you will need to create a custom User model using either the AbstractUser or AbstractBaseUser subclassing.

DjangoX

Installation DjangoX can be installed via Pip, Pipenv, or Docker depending upon your setup. To start, clone the repo to your local computer and change into the proper directory.

    git clone https://github.com/wsvincent/djangox.git
    cd djangox

DjangoX which is my own opinionated starter framework for new Django projects. It comes with a custom user model by default, email/password authentication instead of Django’s default and outdated username/email/password pattern, and is fully extendable with social authentication like Gmail, Facebook, and more.

Time and again on new Django projects I found myself wanting these same basic configuration options yet I’m not aware of an open-source project providing them. The leading Django starter framework is cookiecutter-django and DjangoX is heavily by its example. I learned a lot about Django from reading through the source code. However I often found cookiecutter to be too much for my own personal needs.

There is a temptation for me to continue adding features to DjangoX such as integration with PostgreSQL/Docker, environment variables, third-party packages like django-debug-toolbar and django-extensions, admin hardening, and more. But I want the project to remain accessible to newcomers and flexible enough to support any number of custom configurations beyond what I consider the absolute essentials for any new project.