loading...
Cover image for Make A Custom User Model - Building SaaS #40

Make A Custom User Model - Building SaaS #40

mblayman profile image Matt Layman Originally published at mattlayman.com ・2 min read

Building SaaS (47 Part Series)

1) Building SaaS with Python and Django #1 2) Reporting scraped data - Building SaaS #2 3 ... 45 3) Multiple Stripe plans - Building SaaS #3 4) Using a background scheduler - Building SaaS #4 5) Updating data models - Building SaaS #5 6) Third party integration modeling - Building SaaS #6 7) Displaying third party data - Building SaaS #7 8) Connecting third party services - Building SaaS #8 9) Finishing third party integration - Building SaaS #9 10) Admin dashboards - Building SaaS #10 11) Semi-automated tasks - Building SaaS #11 12) Automation aides - Building SaaS #12 13) Deploying with Ansible - Building SaaS #13 14) Ansible Cranked to 11 - Building SaaS #14 15) Feature Flags with Django Waffle - Building SaaS #15 16) Feature Flags in Action - Building SaaS #16 17) Canceling Stripe Subscriptions - Building SaaS #17 18) Completing Account Deactivation - Building SaaS #18 19) Pip-tools and App Packaging - Building SaaS #19 20) Making a Shiv App - Building SaaS #20 21) Shiv zipapps and CI - Building SaaS #21 22) Upload to S3 with CircleCI orbs - Building SaaS #22 23) It's Alive! A Django Shiv app - Building SaaS #23 24) In the Guts of a Shiv App - Building SaaS #24 25) It's Permissions, Dummy! - Building SaaS #25 26) Connecting Shiv Apps with Ansible - Building SaaS #26 27) Plug the Shiv App Into Nginx - Building SaaS #27 28) Webpack and collectstatic in CI - Building SaaS #28 29) Add Static Assets to Deployment - Building SaaS #29 30) Ripping Out Node.js - Building SaaS #30 31) Celery In A Shiv App - Building SaaS #31 32) wal-e Postgres Backups - Building SaaS #32 33) Get Out, Git! - Building SaaS #33 34) Bring in the WhiteNoise, Bring in Da Funk - Building SaaS #34 35) Deploying WhiteNoise - Building SaaS #35 36) Configurama - Building SaaS #36 37) Lessons From A Failed SaaS - Building SaaS #37 38) New Project, Who Dis? - Building SaaS #38 39) django-environ and django-debug-toolbar - Building SaaS #39 40) Make A Custom User Model - Building SaaS #40 41) User Accounts With django-allauth - Building SaaS #41 42) Add Styles To Templates - Building SaaS #42 43) Use Tailwind On A Template - Building SaaS #43 44) Fast Forms With UpdateView - Building SaaS #44 45) Templates and Logic - Building SaaS #45 46) A Week At A Time - Building SaaS #46 47) How To Style Sign Up - Building SaaS #47

In this episode, we started a users app and hooked up the custom user model feature of Django to unlock the full extensibility of that model in the future. The stream was cut short this week because of some crashing issues in the OBS streaming software.

The goal of the episode was to add django-allauth so that users can sign into the service with an email and password instead of the default username and password combination. We're using Will Vincent's guide on this topic as an aide for this implementation (thanks Will!).

django-allauth makes it possible to add a variety of authentication options like Google or Facebook sign in. My app is not using those modes, but it is taking advantage of the "email as username" feature that the package can provide.

Before getting to the django-allauth integration, we first needed to create a custom User model. This isn't strictly necessary, but it unlocks a lot of future potential and is a hard change to make later in a project.

I started with the creation of a new Django app for the User model to reside in.

(venv) $ python manage.py startapp users

From there, I discussed some code organization ideas and explained why I'm making the choice to put all of my app code into a single, top-level package called homeschool. It's mostly a personal preference issue, but I did talk through some justification for my decision.

(venv) $ mkdir homeschool
(venv) $ touch homeschool/__init__.py
(venv) $ mv users homeschool

With the app in the location I wanted, we put it in the list of installed applications in the settings.

# project/settings.py

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "homeschool.users",
]

Next, we needed to create the model and set it in the settings.

# homeschool/users/models.py
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    """A custom user for extension"""

# project/settings.py
AUTH_USER_MODEL = "users.User"

Finally, we generate the migration to make use of the setup.

(venv) $ python manage.py makemigrations
(venv) $ python manage.py migrate

Unfortunately, that's where the stream ended for the night. My recent upgrade to macOS Catalina was not playing nicely with the OBS streaming software and OBS crashed hard. Interestingly, I was able to get OBS to crash every time I tried to log in to the Django admin in Firefox. It was super weird.

Next time, we will continue to work through the django-allauth addition with a (hopefully) more stable streaming setup.

This article first appeared on mattlayman.com.

Building SaaS (47 Part Series)

1) Building SaaS with Python and Django #1 2) Reporting scraped data - Building SaaS #2 3 ... 45 3) Multiple Stripe plans - Building SaaS #3 4) Using a background scheduler - Building SaaS #4 5) Updating data models - Building SaaS #5 6) Third party integration modeling - Building SaaS #6 7) Displaying third party data - Building SaaS #7 8) Connecting third party services - Building SaaS #8 9) Finishing third party integration - Building SaaS #9 10) Admin dashboards - Building SaaS #10 11) Semi-automated tasks - Building SaaS #11 12) Automation aides - Building SaaS #12 13) Deploying with Ansible - Building SaaS #13 14) Ansible Cranked to 11 - Building SaaS #14 15) Feature Flags with Django Waffle - Building SaaS #15 16) Feature Flags in Action - Building SaaS #16 17) Canceling Stripe Subscriptions - Building SaaS #17 18) Completing Account Deactivation - Building SaaS #18 19) Pip-tools and App Packaging - Building SaaS #19 20) Making a Shiv App - Building SaaS #20 21) Shiv zipapps and CI - Building SaaS #21 22) Upload to S3 with CircleCI orbs - Building SaaS #22 23) It's Alive! A Django Shiv app - Building SaaS #23 24) In the Guts of a Shiv App - Building SaaS #24 25) It's Permissions, Dummy! - Building SaaS #25 26) Connecting Shiv Apps with Ansible - Building SaaS #26 27) Plug the Shiv App Into Nginx - Building SaaS #27 28) Webpack and collectstatic in CI - Building SaaS #28 29) Add Static Assets to Deployment - Building SaaS #29 30) Ripping Out Node.js - Building SaaS #30 31) Celery In A Shiv App - Building SaaS #31 32) wal-e Postgres Backups - Building SaaS #32 33) Get Out, Git! - Building SaaS #33 34) Bring in the WhiteNoise, Bring in Da Funk - Building SaaS #34 35) Deploying WhiteNoise - Building SaaS #35 36) Configurama - Building SaaS #36 37) Lessons From A Failed SaaS - Building SaaS #37 38) New Project, Who Dis? - Building SaaS #38 39) django-environ and django-debug-toolbar - Building SaaS #39 40) Make A Custom User Model - Building SaaS #40 41) User Accounts With django-allauth - Building SaaS #41 42) Add Styles To Templates - Building SaaS #42 43) Use Tailwind On A Template - Building SaaS #43 44) Fast Forms With UpdateView - Building SaaS #44 45) Templates and Logic - Building SaaS #45 46) A Week At A Time - Building SaaS #46 47) How To Style Sign Up - Building SaaS #47

Posted on Jul 2 by:

mblayman profile

Matt Layman

@mblayman

Matt Layman is a software engineer from Frederick, MD. He is an open source software maintainer and advocate for Python.

Discussion

markdown guide