From 7bf9717925707e5ec7c122785b7fa21bf4e8cd1d Mon Sep 17 00:00:00 2001 From: Penny Date: Sun, 3 Nov 2024 18:37:49 +0900 Subject: [PATCH] implement user model --- app/app/settings.py | 2 ++ app/core/migrations/0001_initial.py | 32 ++++++++++++++++++++++++++++ app/core/models.py | 33 +++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 app/core/migrations/0001_initial.py diff --git a/app/app/settings.py b/app/app/settings.py index 56cadcd..d36e749 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -128,3 +128,5 @@ # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +AUTH_USER_MODEL = 'core.User' diff --git a/app/core/migrations/0001_initial.py b/app/core/migrations/0001_initial.py new file mode 100644 index 0000000..b2a4170 --- /dev/null +++ b/app/core/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 3.2.25 on 2024-11-03 09:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + 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')), + ('email', models.EmailField(max_length=255, unique=True)), + ('name', models.CharField(max_length=255)), + ('is_active', models.BooleanField(default=False)), + ('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={ + 'abstract': False, + }, + ), + ] diff --git a/app/core/models.py b/app/core/models.py index 56b48a5..0edd236 100644 --- a/app/core/models.py +++ b/app/core/models.py @@ -1,3 +1,32 @@ -from django.db import models # noqa +""" +Database models. +""" +from django.db import models +from django.contrib.auth.models import ( + AbstractBaseUser, + BaseUserManager, + PermissionsMixin, +) -# Create your models here. + +class UserManager(BaseUserManager): + """Manager for users.""" + + def create_user(self, email, password=None, **extra_fields): + user = self.model(email=email, **extra_fields) + user.set_password(password) + user.save(using=self._db) + + return user + + +class User(AbstractBaseUser, PermissionsMixin): + """User in the system.""" + email = models.EmailField(max_length=255, unique=True) + name = models.CharField(max_length=255) + is_active = models.BooleanField(default=True) + is_active = models.BooleanField(default=False) + + objects = UserManager() + + USERNAME_FIELD = 'email'