I. Introduction
After learning the previous article "Dragonboard410c Server Series Six User Interaction System", bloggers believe that everyone can master the Django framework based web server construction and simple data interaction. But, deep down, how does Django store interactive data for different pages? I believe that children's shoes that have seen the code uploaded by the bloggers are already known. This is also the topic we are discussing today: the database.
Database
1. Concept:
A database is a repository built on a computer storage device that organizes, stores, and manages data according to a data structure. In simple terms, it can be regarded as an electronic file cabinet—where the electronic file is stored, the user can add, intercept, update, delete, etc. the data in the file. In the daily work of economic management, it is often necessary to put some relevant data into such a "warehouse" and handle it accordingly according to the needs of management. It can be said that the design of the database is the top priority of the web back-end architecture design. A reasonable database design will save a lot of computing steps and improve the overall operating efficiency for the background, which is also an important criterion for measuring the background of a background engineer.
2. Django's database:
2.1. Introduction:
Django database, we call it model, database-related code is generally written in models.py, Django supports sqlite3, MySQL, PostgreSQL and other databases, only need to be configured in setTIngs.py, without changing models.py The code, the rich API is extremely convenient to use. Of course, we can also customize filed according to our own needs, which is our own model. For example, in the user interaction system of the previous article, we not only inherit the original MyUser, but also customize the model of the message: online_message and the state model of the system: online_SystemStatus.
2.2 Creation steps:
2.2.1. Create a django project:
Django-admin startproject test
2.2.2. Create a webapp:
Cd test
Django-admin startapp sql
2.2.3. Add webapp to the test project:
Vim test/test/setTIngs.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staTIcfiles',
'sql',
)
2.2.4. Define the data model of sql:
From django.db import models
Class Person(models.Model):
Name = models.CharField(max_length=30)
Age = models.IntegerField()
2.2.5. Initialize database data:
Cd ~/test
Python manage.py makemigraTIons sql
Python manage.py migrate
3. Instance model code:
#coding:utf8
From django.db import models
From django.contrib.auth.models import (
BaseUserManager , AbstractBaseUser , PermissionsMixin )
Class MyUserManager ( BaseUserManager ):
# def _create_user(self, username, email, password, **extra_fields):
Def _create_user ( self , username , password , ** extra_fields ):
"""
Creates and saves a User with the given username, email and password.
"""
If not username :
Raise ValueError ( 'The given username must be set' )
# email = self.normalize_email(email)
# user = self.model(username=username, email=email, **extra_fields)
User = self . model ( username = username , ** extra_fields )
User . set_password ( password )
User . save ( using = self . _db )
Return user
# def create_user(self, username, email, password, **extra_fields):
Def create_user ( self , username , password , ** extra_fields ):
Extra_fields . setdefault ( 'is_staff' , False )
Extra_fields . setdefault ( 'email' , "" )
Extra_fields . setdefault ( 'phone' , "" )
Extra_fields . setdefault ( 'haarValue' , "" )
Extra_fields . setdefault ( 'avatarImg' , "" )
# extra_fields.setdefault('text',"")
# extra_fields.setdefault('video',"")
# extra_fields.setdefault('time',"")
Return self . _create_user ( username , password , ** extra_fields )
# return self._create_user(username, email, password, **extra_fields)
# def create_superuser(self, username, email, password, **extra_fields):
Def create_superuser ( self , username , password , ** extra_fields ):
Extra_fields . setdefault ( 'is_staff' , True )
If extra_fields . get ( 'is_staff' ) is not True :
raise ValueError ( 'Superuser must have is_staff = True')
#拓展user data by xiao
#get
#to
#message_flag
#text
#video
#time
# extra_fields.setdefault('got',username)
# extra_fields.setdefault('to',username)
# extra_fields.setdefault('message_flag',"1")
# extra_fields.setdefault('text',"")
# extra_fields.setdefault('video',"")
# extra_fields.setdefault('time',"")
Return self . _create_user ( username , password , ** extra_fields )
# return self._create_user(username, email, password, **extra_fields)
Â
Class MyUser ( AbstractBaseUser , PermissionsMixin ):
Username = models . CharField ( max_length = 254 , unique = True , db_index = True )
Email = models . CharField ( max_length = 254 )
Phone = models . CharField ( max_length = 254 )
haarValue = models . CharField ( max_length = 254 )
avatarImg = models . CharField ( max_length = 254 )
#email = models.EmailField('email address', max_length=254)
#text=models.CharField(max_length=254, unique=True, db_index=True)
Is_staff = models . BooleanField ( 'staff status' , default = False )
# userID = models.IntegerField(default=1) #User-specific ID
# is_active = models.BooleanField('active', default=True)
# got = models.CharField(max_length=254,default=username)
# to = models.CharField(max_length=254,default=username)
# message_flag = models.CharField(max_length=254, default=1)
# text = models.CharField(max_length=254, default="")
# video = models.CharField(max_length=254,default="")
# time = models.CharField(max_length=254,default="")
USERNAME_FIELD = 'username'
# REQUIRED_FIELDS = ['email']
Objects = MyUserManager ()
Â
Class Meta :
Db_table = 'myuser'
# def get_full_name(self):
# return self.username
# def get_short_name(self):
# return self.username
#Build a model for pushing messages
Class Message ( models . Model ):
# username = models.CharField(max_length=254) #消æ¯æŽ¨è€…'s name
ownerID = models . IntegerField ( default = 1 )
pushID = models . IntegerField ( default = 1 )
infoType = models . IntegerField ( default = 0 )
infoSubject = models . CharField ( max_length = 254 )
infoContent = models . CharField ( max_length = 254 )
filePath = models . CharField ( max_length = 1000 )
pushTim = models . CharField ( max_length = 254 )
infoValidityTime = models . CharField ( max_length = 254 )
isTop = models . IntegerField ( default = 0 )
viewWeight = models . IntegerField ( default = 1 )
Def __unicode__ ( self ):
# Using def __str__(self) in Python 3
Return self . name
Class SystemStatus ( models . Model ): #Instance object: online_message MMDB.updateSystemStatus('peopleInfront',-1,0,'no people in front')
statusName = models . CharField ( max_length = 30 , default = "peopleInfront" )
statusValue = models . IntegerField ( default =- 1 )
statusParam = models . IntegerField ( default = 0 )
statusDiscirption = models . CharField ( max_length = 50 , default = "no people in front" )
Â
Def __unicode__ ( self ):
# Using def __str__(self) in Python 3
Return self . name
3. Routine sharing: http://pan.baidu.com/share/link? Shareid=3536829173&uk=1812979481
For Oppo Glass
For Oppo Glass,Oppo Find X3 Front Glass,Oppo Phone Screen Glass,Oppo Reno 5 Pro Glass
Dongguan Jili Electronic Technology Co., Ltd. , https://www.jlglassoca.com