Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
38 views3 pages

Django Instructions

The document describes how to create a Django project and app, and includes models and admin configuration for a User model. Key steps include: 1) Using django-admin to start a new project, create an app, make migrations, and run the server. 2) The User model includes fields like phone, name, address and custom manager and methods for authentication. 3) Signals are used to send a verification code when a new user is created. 4) The admin config customizes the UserAdmin for fields and list display of the User model.

Uploaded by

Jojo Madready
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Django Instructions

The document describes how to create a Django project and app, and includes models and admin configuration for a User model. Key steps include: 1) Using django-admin to start a new project, create an app, make migrations, and run the server. 2) The User model includes fields like phone, name, address and custom manager and methods for authentication. 3) Signals are used to send a verification code when a new user is created. 4) The admin config customizes the UserAdmin for fields and list display of the User model.

Uploaded by

Jojo Madready
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

create python django project

pip install django - install global django


django-admin startproject Name - create new project
cd Name - enter file directory
python -m venv venv - install local venv
pip install django - install local django
python manage.py makemigrations - check database
python manage.py migrate - save database
python manage.py createsuperuser - create superuser (super admin)
venv/Scripts/activate - activate venv
python manage.py runsever - start server
python manage.py startapp AppName - create new app

**********admin Model************

from django.db import models


from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db.models.signals import post_save
import random, uuid, os
from django.utils.translation import ugettext_lazy as _
from ..helper.models import TimeStamp, Region
from ..notification.models import send_sms_to_phone

class UserManager(BaseUserManager):
use_in_migrations = True

def _create_user(self, phone, password, **extra_fields):


user = self.model(phone=phone, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, phone, password=None, **extra_fields):


extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(phone, password, **extra_fields)

def create_superuser(self, phone, password, **extra_fields):


if password is None:
raise TypeError(
'Superuser must have a password'
)
user = self.create_user(phone=phone, password=password, **extra_fields)

user.is_superuser = True
user.is_staff = True
user.save(using=self._db)

return user

def get_avatar(instance, filename):


ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return os.path.join('account', filename)

class User(AbstractUser):
CHOICE_TYPE = (
(1, _("Begin")),
(2, _("Check Sms")),
(3, _("Succes"))
)
GENDER_TYPE = (
(1, _("Man")),
(2, _("Woman"))
)
middle_name = models.CharField(blank=True, null=True, max_length=250)
username = models.CharField(blank=True, null=True, max_length=50)
phone = models.CharField(max_length=13, unique=True)
region = models.ForeignKey(Region, blank=True, null=True,
related_name='region_users', on_delete=models.DO_NOTHING)
city = models.ForeignKey(Region, blank=True, null=True,
related_name='city_users', on_delete=models.DO_NOTHING)
address = models.TextField(null=True, blank=True)
avatar = models.ImageField(upload_to=get_avatar,
default='default__images/user.png', null=True, blank=True)
birthday = models.DateField(null=True, blank=True)
gender = models.IntegerField(choices=GENDER_TYPE, default=1)
verified = models.IntegerField(choices=CHOICE_TYPE, default=1)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(auto_now_add=True)
code = models.CharField(max_length=10, null=True, blank=True)
firebase_token = models.CharField(max_length=255, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
USERNAME_FIELD = 'phone'
objects = UserManager()
REQUIRED_FIELDS = ['email', 'first_name', 'last_name']

def __str__(self):
return 'User — ' + self.phone

def save(self, *args, **kwargs):


if self.code is None:
self.code = generate_code(self.phone)
super().save()

def generate_code(phone):
if phone:
code = random.randint(9999, 99999)
return code
return False

def verification_code_saver(sender, instance, **kwargs):


if kwargs['created']:
phone = instance.phone
code = instance.code
send_sms_to_phone(phone, 'Tasdiqlash kodi: '+str(code))

post_save.connect(receiver=verification_code_saver, sender=User)

***************admin.py*******************
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
from .models import User
from related_admin import RelatedFieldAdmin

class UserAdmin(DjangoUserAdmin, admin.ModelAdmin):


fieldsets = (
(_('Main'), {'fields': ('phone', 'password', 'code', 'verified')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'middle_name',
'birthday', 'gender',
'email', 'region', 'city', 'address',
'avatar', 'firebase_token')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('phone', 'password1', 'password2', 'verified'),
}),
)
list_display = ('phone', 'get_fullname', 'is_staff', 'date_joined', 'verified',
'created_at')
search_fields = ('email', 'first_name', 'last_name', 'phone',)
ordering = ('id',)
list_filter = (
'verified', 'is_active', 'is_staff', 'is_superuser',
)

@staticmethod
def get_fullname(obj):
return "{} {}".format(obj.first_name, obj.last_name)

admin.site.register(User, UserAdmin)

You might also like