View on GitHub

カスタムユーザモデルの定義

Home

カスタムユーザモデルの定義

モデル

src/backend/api/accounts/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    """
    カスタムユーザモデル
    """
    pass

src/backend/config/settings/base.py

......
ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY')
AUTH_USER_MODEL = 'accounts.CustomUser'  # <- 追加

マイグレーション

(recsys_full) backend$
 export DB_USER=rsl
 export DB_PASSWORD=rsl-pass

(recsys_full) backend$ python manage.py makemigrations accounts
Migrations for 'accounts':
  api/accounts/migrations/0001_initial.py
    + Create model CustomUser

(recsys_full) backend$ python manage.py sqlmigrate accounts 0001
BEGIN;
--
-- Create model CustomUser
--
CREATE TABLE "accounts_customuser" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "password" varchar(128) NOT NULL, "last_login" timestamp with time zone NULL, "is_superuser" boolean NOT NULL, "username" varchar(150) NOT NULL UNIQUE, "first_name" varchar(150) NOT NULL, "last_name" varchar(150) NOT NULL, "email" varchar(254) NOT NULL, "is_staff" boolean NOT NULL, "is_active" boolean NOT NULL, "date_joined" timestamp with time zone NOT NULL);
CREATE TABLE "accounts_customuser_groups" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "customuser_id" bigint NOT NULL, "group_id" integer NOT NULL);
CREATE TABLE "accounts_customuser_user_permissions" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "customuser_id" bigint NOT NULL, "permission_id" integer NOT NULL);
CREATE INDEX "accounts_customuser_username_722f3555_like" ON "accounts_customuser" ("username" varchar_pattern_ops);
ALTER TABLE "accounts_customuser_groups" ADD CONSTRAINT "accounts_customuser_groups_customuser_id_group_id_c074bdcb_uniq" UNIQUE ("customuser_id", "group_id");
ALTER TABLE "accounts_customuser_groups" ADD CONSTRAINT "accounts_customuser__customuser_id_bc55088e_fk_accounts_" FOREIGN KEY ("customuser_id") REFERENCES "accounts_customuser" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "accounts_customuser_groups" ADD CONSTRAINT "accounts_customuser_groups_group_id_86ba5f9e_fk_auth_group_id" FOREIGN KEY ("group_id") REFERENCES "auth_group" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "accounts_customuser_groups_customuser_id_bc55088e" ON "accounts_customuser_groups" ("customuser_id");
CREATE INDEX "accounts_customuser_groups_group_id_86ba5f9e" ON "accounts_customuser_groups" ("group_id");
ALTER TABLE "accounts_customuser_user_permissions" ADD CONSTRAINT "accounts_customuser_user_customuser_id_permission_9632a709_uniq" UNIQUE ("customuser_id", "permission_id");
ALTER TABLE "accounts_customuser_user_permissions" ADD CONSTRAINT "accounts_customuser__customuser_id_0deaefae_fk_accounts_" FOREIGN KEY ("customuser_id") REFERENCES "accounts_customuser" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "accounts_customuser_user_permissions" ADD CONSTRAINT "accounts_customuser__permission_id_aea3d0e5_fk_auth_perm" FOREIGN KEY ("permission_id") REFERENCES "auth_permission" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "accounts_customuser_user_permissions_customuser_id_0deaefae" ON "accounts_customuser_user_permissions" ("customuser_id");
CREATE INDEX "accounts_customuser_user_permissions_permission_id_aea3d0e5" ON "accounts_customuser_user_permissions" ("permission_id");
COMMIT;

(recsys_full) backend$ python manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying accounts.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying sessions.0001_initial... OK

テーブルの確認

recsys_full=# \dt
                       List of relations
 Schema |                 Name                 | Type  | Owner
--------+--------------------------------------+-------+-------
 public | accounts_customuser                  | table | rsl
 public | accounts_customuser_groups           | table | rsl
 public | accounts_customuser_user_permissions | table | rsl
 public | auth_group                           | table | rsl
 public | auth_group_permissions               | table | rsl
 public | auth_permission                      | table | rsl
 public | django_admin_log                     | table | rsl
 public | django_content_type                  | table | rsl
 public | django_migrations                    | table | rsl
 public | django_session                       | table | rsl
(10 rows)

recsys_full=# \d accounts_customuser
                                Table "public.accounts_customuser"
    Column    |           Type           | Collation | Nullable |             Default
--------------+--------------------------+-----------+----------+----------------------------------
 id           | bigint                   |           | not null | generated by default as identity
 password     | character varying(128)   |           | not null |
 last_login   | timestamp with time zone |           |          |
 is_superuser | boolean                  |           | not null |
 username     | character varying(150)   |           | not null |
 first_name   | character varying(150)   |           | not null |
 last_name    | character varying(150)   |           | not null |
 email        | character varying(254)   |           | not null |
 is_staff     | boolean                  |           | not null |
 is_active    | boolean                  |           | not null |
 date_joined  | timestamp with time zone |           | not null |
Indexes:
    "accounts_customuser_pkey" PRIMARY KEY, btree (id)
    "accounts_customuser_username_722f3555_like" btree (username varchar_pattern_ops)
    "accounts_customuser_username_key" UNIQUE CONSTRAINT, btree (username)
Referenced by:
    TABLE "accounts_customuser_user_permissions" CONSTRAINT "accounts_customuser__customuser_id_0deaefae_fk_accounts_" FOREIGN KEY (customuser_id) REFERENCES accounts_customuser(id) DEFERRABLE INITIALLY DEFERRED
    TABLE "accounts_customuser_groups" CONSTRAINT "accounts_customuser__customuser_id_bc55088e_fk_accounts_" FOREIGN KEY (customuser_id) REFERENCES accounts_customuser(id) DEFERRABLE INITIALLY DEFERRED
    TABLE "django_admin_log" CONSTRAINT "django_admin_log_user_id_c564eba6_fk_accounts_customuser_id" FOREIGN KEY (user_id) REFERENCES accounts_customuser(id) DEFERRABLE INITIALLY DEFERRED

参考

  1. 現場で使える Django の教科書《基礎編》 # 第 11 章 データベースのマイグレーション