Skip to content

Commit 62ba021

Browse files
authored
Add custom user model (#2015)
* Adde custom user model * Use users.apps.UsersConfig
1 parent 38c8525 commit 62ba021

File tree

4 files changed

+179
-6
lines changed

4 files changed

+179
-6
lines changed

main/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
ALLOWED_HOSTS = ["*"]
7171

72-
AUTH_USER_MODEL = "auth.User"
72+
AUTH_USER_MODEL = "users.User"
7373

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

@@ -107,7 +107,7 @@
107107
"drf_spectacular",
108108
# Put our apps after this point
109109
"main",
110-
"users",
110+
"users.apps.UsersConfig",
111111
"authentication",
112112
"channels",
113113
"profiles",

users/migrations/0001_initial.py

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,143 @@
1-
# Generated by Django 4.2.18 on 2025-02-05 19:41
1+
# Generated by Django 4.2.18 on 2025-02-05 20:42
22

3-
from django.db import migrations
3+
import django.contrib.auth.models
4+
import django.contrib.auth.validators
5+
import django.utils.timezone
6+
from django.db import migrations, models
47

58

69
class Migration(migrations.Migration):
7-
dependencies = []
10+
initial = True
811

9-
operations = []
12+
dependencies = [
13+
("auth", "0012_alter_user_first_name_max_length"),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name="User",
19+
fields=[
20+
(
21+
"id",
22+
models.BigAutoField(
23+
auto_created=True,
24+
primary_key=True,
25+
serialize=False,
26+
verbose_name="ID",
27+
),
28+
),
29+
("password", models.CharField(max_length=128, verbose_name="password")),
30+
(
31+
"last_login",
32+
models.DateTimeField(
33+
blank=True, null=True, verbose_name="last login"
34+
),
35+
),
36+
(
37+
"is_superuser",
38+
models.BooleanField(
39+
default=False,
40+
help_text=(
41+
"Designates that this user has all permissions without "
42+
"explicitly assigning them."
43+
),
44+
verbose_name="superuser status",
45+
),
46+
),
47+
(
48+
"username",
49+
models.CharField(
50+
error_messages={
51+
"unique": "A user with that username already exists."
52+
},
53+
help_text=(
54+
"Required. 150 characters or fewer. "
55+
"Letters, digits and @/./+/-/_ only."
56+
),
57+
max_length=150,
58+
unique=True,
59+
validators=[
60+
django.contrib.auth.validators.UnicodeUsernameValidator()
61+
],
62+
verbose_name="username",
63+
),
64+
),
65+
(
66+
"first_name",
67+
models.CharField(
68+
blank=True, max_length=150, verbose_name="first name"
69+
),
70+
),
71+
(
72+
"last_name",
73+
models.CharField(
74+
blank=True, max_length=150, verbose_name="last name"
75+
),
76+
),
77+
(
78+
"email",
79+
models.EmailField(
80+
blank=True, max_length=254, verbose_name="email address"
81+
),
82+
),
83+
(
84+
"is_staff",
85+
models.BooleanField(
86+
default=False,
87+
help_text=(
88+
"Designates whether the user can log into this admin site."
89+
),
90+
verbose_name="staff status",
91+
),
92+
),
93+
(
94+
"is_active",
95+
models.BooleanField(
96+
default=True,
97+
help_text=(
98+
"Designates whether this user should be treated as active. "
99+
"Unselect this instead of deleting accounts."
100+
),
101+
verbose_name="active",
102+
),
103+
),
104+
(
105+
"date_joined",
106+
models.DateTimeField(
107+
default=django.utils.timezone.now, verbose_name="date joined"
108+
),
109+
),
110+
(
111+
"groups",
112+
models.ManyToManyField(
113+
blank=True,
114+
help_text=(
115+
"The groups this user belongs to. A user will get all "
116+
"permissions granted to each of their groups."
117+
),
118+
related_name="user_set",
119+
related_query_name="user",
120+
to="auth.group",
121+
verbose_name="groups",
122+
),
123+
),
124+
(
125+
"user_permissions",
126+
models.ManyToManyField(
127+
blank=True,
128+
help_text="Specific permissions for this user.",
129+
related_name="user_set",
130+
related_query_name="user",
131+
to="auth.permission",
132+
verbose_name="user permissions",
133+
),
134+
),
135+
],
136+
options={
137+
"db_table": "auth_user",
138+
},
139+
managers=[
140+
("objects", django.contrib.auth.models.UserManager()),
141+
],
142+
),
143+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 4.2.18 on 2025-02-05 22:13
2+
3+
from django.db import migrations
4+
5+
6+
def change_user_type(apps, schema_editor):
7+
"""Update the ContentType for User to point to the custom one"""
8+
ContentType = apps.get_model("contenttypes", "ContentType")
9+
ct = ContentType.objects.filter(app_label="auth", model="user").first()
10+
if ct:
11+
ct.app_label = "users"
12+
ct.save()
13+
14+
15+
def revert_user_type(apps, schema_editor):
16+
"""Update the ContentType for User to point to the default django one"""
17+
ContentType = apps.get_model("contenttypes", "ContentType")
18+
ct = ContentType.objects.filter(app_label="users", model="user").first()
19+
if ct:
20+
ct.app_label = "auth"
21+
ct.save()
22+
23+
24+
class Migration(migrations.Migration):
25+
dependencies = [
26+
("users", "0001_initial"),
27+
]
28+
29+
operations = [migrations.RunPython(change_user_type, revert_user_type)]

users/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Users models"""
2+
3+
from django.contrib.auth.models import AbstractUser
4+
5+
6+
class User(AbstractUser):
7+
"""Custom model for users"""
8+
9+
class Meta:
10+
db_table = "auth_user"

0 commit comments

Comments
 (0)