Skip to content

Commit 0caf214

Browse files
committed
Fixed the accounts model to include an UserManager and rebuild migrations
1 parent a754694 commit 0caf214

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

accounts/migrations/0001_initial.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Generated by Django 2.1.2 on 2018-10-03 02:11
1+
# Generated by Django 2.1.2 on 2018-10-06 15:20
22

3+
import accounts.models
34
import django.contrib.auth.password_validation
45
from django.db import migrations, models
56

@@ -30,5 +31,8 @@ class Migration(migrations.Migration):
3031
options={
3132
'abstract': False,
3233
},
34+
managers=[
35+
('objects', accounts.models.UserManager()),
36+
],
3337
),
3438
]

accounts/models.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
from django.db import models
2-
from django.contrib.auth.models import AbstractBaseUser
2+
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
33
from django.contrib.auth import password_validation
44

5+
class UserManager(BaseUserManager):
6+
use_in_migrations = True
7+
8+
def create_user(self, email, password=None, **kwargs):
9+
user = self.model(email=email, password=password, **kwargs)
10+
11+
user.save(using=self._db)
12+
return user
13+
14+
def create_superuser(self, email, username, password):
15+
user = self.create_user(email, password=password, username=username, is_admin=True)
16+
user.save(using=self._db)
17+
return user
18+
19+
def get_by_natural_key(self, username):
20+
case_insensitive_username_field = '{}__iexact'.format(self.model.USERNAME_FIELD)
21+
return self.get(**{case_insensitive_username_field: username})
522

623
class User(AbstractBaseUser):
724
USERNAME_FIELD = 'username'
825

26+
REQUIRED_FIELDS = ['email']
27+
928
created_at = models.DateTimeField(auto_now_add=True)
1029
updated_at = models.DateTimeField(auto_now=True)
1130
password = models.CharField(
@@ -24,5 +43,11 @@ class User(AbstractBaseUser):
2443
is_admin = models.BooleanField(default=False)
2544
flagged = models.BooleanField(default=False)
2645

46+
objects = UserManager()
47+
2748
def __str__(self):
2849
return self.username
50+
51+
@property
52+
def is_staff(self):
53+
return self.is_admin

0 commit comments

Comments
 (0)