Cinquième étape créant l'application Advance dans Python Django
// creating advance application for custom user model
// create a new app - go to terminal
python manage.py startapp accounts
// go to settings.py add the code below
INSTALLED_APPS = [
'accounts',
]
// go to - accounts folder ( new app ) - models.py
// paste the code below
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
// Create your models here.
class MyAccountManager(BaseUserManager):
def create_user(self, first_name, last_name, username, email, password=None):
if not email:
raise ValueError('User must have an email address')
if not username:
raise ValueError('User must have a username')
user = self.model(
email = self.normalize_email(email),
username = username,
first_name = first_name,
last_name = last_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, first_name, last_name, email, username, password):
user = self.create_user(
email = self.normalize_email(email),
username = username,
password = password,
first_name = first_name,
last_name = last_name,
)
user.is_admin = True
user.is_active = True
user.is_staff = True
user.is_superadmin = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=50, unique=True)
email = models.EmailField(max_length=100, unique=True)
phone_number = models.CharField(max_length=50)
//REQUIRED
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superadmin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name' ]
objects = MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, add_label):
return True
// go to settings.py and paste the code below
// put the code below next to
// WSGI_APPLICATION = 'mainControl.wsgi.application'
AUTH_USER_MODEL = 'accounts.Account'
// go to Accounts folder ( new app ) admin.py
// paste the codes below
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Account
class AccountAdmin(UserAdmin):
list_display = ('email', 'first_name', 'last_name', 'username', 'last_login', 'is_active', 'date_joined')
list_display_links = ('email', 'first_name', 'last_name')
readonly_fields = ('last_login', 'date_joined')
ordering = ('-date_joined',)
filter_horizontal = ()
list_filter = ()
fieldsets = ()
admin.site.register(Account, AccountAdmin)
// delete the default database
db.sqlite3
// go to category - migration folder delete the following
0001_initial.py
0002_auto_20201005_0245.py
//---------------------------------
// run server again
python manage.py runserver
// after new database created ( db.sqlite3 )
python manage.py makemigration
// after if successful run
python manage.py migrate
// create a new superuser
// if you are using GITBASH TERMINAL
winpty python manage.py createsuperuser
// if you are using EDITER ternimal
python manage.py createsuperuser
// go to /admin/ again and try to login
// if successfully logged in, congrats
BenLeolam