Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Fifth step Creating Advance app in 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 
Comment

PREVIOUS NEXT
Code Example
Python :: python glob sort numerically 
Python :: python print over the same line 
Python :: typing return two objects 
Python :: python django creating products 
Python :: explained_variance_ratio kernel pca 
Python :: print prime nos from 1 to n 
Python :: nltk python text from url 
Python :: udp client server chat program in python 
Python :: sublime python input 
Python :: #clearing all keys new key in python 
Python :: databases not showing in odoo 13 
Python :: plotly change marker symboly sequence 
Python :: how to solve spacy no model en 
Python :: channel unlock command in discord.py 
Python :: sum of two diagonals in matrix 
Python :: capitalise.texts 
Python :: host python discord bot free 
Python :: python record screen and audio 
Python :: Another example: using a colorbar to show bar height 
Python :: How to count a consecutive series of positive or negative values in a column in python 
Python :: cvhaardetectobjects 
Python :: yield value from csv file python 
Python :: ec2 ssh terminal hangs after sometime 
Python :: 2600/6 
Python :: add sign to y axis values python 
Python :: dip programming language 
Python :: removing stop words in python 
Python :: matplotlib FiveThirtyEight creating a signature 
Python :: pandas normalize rows to max value 
Python :: get decimal value of char python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =