Skip to content

Commit

Permalink
add superuser support
Browse files Browse the repository at this point in the history
  • Loading branch information
ytliuSVN committed Nov 9, 2024
1 parent e46a7e5 commit e51f49b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/core/migrations/0002_auto_20241108_1325.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-11-08 13:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='user',
name='is_staff',
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name='user',
name='is_active',
field=models.BooleanField(default=True),
),
]
10 changes: 9 additions & 1 deletion app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ def create_user(self, email, password=None, **extra_fields):

return user

def create_superuser(self, email, password):
user = self.create_user(email, password)
user.is_staff = True
user.is_superuser = True
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)
is_staff = models.BooleanField(default=False)

objects = UserManager()

Expand Down
9 changes: 9 additions & 0 deletions app/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ def test_new_user_email_normalized(self):
def test_new_user_without_email_raises_error(self):
with self.assertRaises(ValueError):
get_user_model().objects.create_user('', 'sample123')

def test_create_superuser(self):
user = get_user_model().objects.create_superuser(
'[email protected]',
'test123'
)

self.assertTrue(user.is_superuser)
self.assertTrue(user.is_staff)

0 comments on commit e51f49b

Please sign in to comment.