Skip to content

Add custom user model #2015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

ALLOWED_HOSTS = ["*"]

AUTH_USER_MODEL = "auth.User"
AUTH_USER_MODEL = "users.User"

SECURE_SSL_REDIRECT = get_bool("MITOL_SECURE_SSL_REDIRECT", True) # noqa: FBT003

Expand Down Expand Up @@ -107,7 +107,7 @@
"drf_spectacular",
# Put our apps after this point
"main",
"users",
"users.apps.UsersConfig",
"authentication",
"channels",
"profiles",
Expand Down
142 changes: 138 additions & 4 deletions users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,143 @@
# Generated by Django 4.2.18 on 2025-02-05 19:41
# Generated by Django 4.2.18 on 2025-02-05 20:42

from django.db import migrations
import django.contrib.auth.models
import django.contrib.auth.validators
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = []
initial = True

operations = []
dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.CreateModel(
name="User",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
(
"is_superuser",
models.BooleanField(
default=False,
help_text=(
"Designates that this user has all permissions without "
"explicitly assigning them."
),
verbose_name="superuser status",
),
),
(
"username",
models.CharField(
error_messages={
"unique": "A user with that username already exists."
},
help_text=(
"Required. 150 characters or fewer. "
"Letters, digits and @/./+/-/_ only."
),
max_length=150,
unique=True,
validators=[
django.contrib.auth.validators.UnicodeUsernameValidator()
],
verbose_name="username",
),
),
(
"first_name",
models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
),
(
"last_name",
models.CharField(
blank=True, max_length=150, verbose_name="last name"
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text=(
"Designates whether the user can log into this admin site."
),
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text=(
"Designates whether this user should be treated as active. "
"Unselect this instead of deleting accounts."
),
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
(
"groups",
models.ManyToManyField(
blank=True,
help_text=(
"The groups this user belongs to. A user will get all "
"permissions granted to each of their groups."
),
related_name="user_set",
related_query_name="user",
to="auth.group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.permission",
verbose_name="user permissions",
),
),
],
options={
"db_table": "auth_user",
},
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
),
]
29 changes: 29 additions & 0 deletions users/migrations/0002_update_user_contenttype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.18 on 2025-02-05 22:13

from django.db import migrations


def change_user_type(apps, schema_editor):
"""Update the ContentType for User to point to the custom one"""
ContentType = apps.get_model("contenttypes", "ContentType")
ct = ContentType.objects.filter(app_label="auth", model="user").first()
if ct:
ct.app_label = "users"
ct.save()


def revert_user_type(apps, schema_editor):
"""Update the ContentType for User to point to the default django one"""
ContentType = apps.get_model("contenttypes", "ContentType")
ct = ContentType.objects.filter(app_label="users", model="user").first()
if ct:
ct.app_label = "auth"
ct.save()


class Migration(migrations.Migration):
dependencies = [
("users", "0001_initial"),
]

operations = [migrations.RunPython(change_user_type, revert_user_type)]
10 changes: 10 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Users models"""

from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
"""Custom model for users"""

class Meta:
db_table = "auth_user"
Loading