解析Dragonboard 410c搭載web服務(wù)器之?dāng)?shù)據(jù)庫(kù)的設(shè)計(jì)
一.前言
本文引用地址:http://cafeforensic.com/article/201808/385374.htm在經(jīng)過(guò)上篇《Dragonboard410c 服務(wù)器系列六之用戶(hù)交互系統(tǒng)》的學(xué)習(xí),博主相信大家都能大致掌握Django框架基礎(chǔ)的web服務(wù)器搭建以及簡(jiǎn)單的數(shù)據(jù)交互。但是,往深處想,Django是如何存儲(chǔ)不同頁(yè)面的交互數(shù)據(jù)的?相信有看過(guò)博主之前上傳的代碼的童鞋都已經(jīng)知曉,這也是我們今天要討論的主題:數(shù)據(jù)庫(kù)。
二.數(shù)據(jù)庫(kù)
1.概念:
數(shù)據(jù)庫(kù)(Database)是按照數(shù)據(jù)結(jié)構(gòu)來(lái)組織、存儲(chǔ)和管理數(shù)據(jù)的建立在計(jì)算機(jī)存儲(chǔ)設(shè)備上的倉(cāng)庫(kù)。簡(jiǎn)單來(lái)說(shuō)是本身可視為電子化的文件柜——存儲(chǔ)電子文件的處所,用戶(hù)可以對(duì)文件中的數(shù)據(jù)進(jìn)行新增、截取、更新、刪除等操作。在經(jīng)濟(jì)管理的日常工作中,常常需要把某些相關(guān)的數(shù)據(jù)放進(jìn)這樣的“倉(cāng)庫(kù)”,并根據(jù)管理的需要進(jìn)行相應(yīng)的處理??梢哉f(shuō)數(shù)據(jù)庫(kù)的設(shè)計(jì)是web后臺(tái)架構(gòu)設(shè)計(jì)的重中之重,合理的數(shù)據(jù)庫(kù)設(shè)計(jì)將會(huì)為后臺(tái)節(jié)省大量的運(yùn)算步驟和提高整體的運(yùn)行效率,這也是衡量一個(gè)后臺(tái)工程師功底的重要標(biāo)準(zhǔn)。
2.Django的數(shù)據(jù)庫(kù):
2.1.介紹:
Django的數(shù)據(jù)庫(kù),我們稱(chēng)之為模型,與數(shù)據(jù)庫(kù)相關(guān)的代碼一般寫(xiě)在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等數(shù)據(jù)庫(kù),只需要在setTIngs.py中配置即可,不用更改models.py中的代碼,豐富的API極大的方便了使用。當(dāng)然,我們也可以根據(jù)自己的需要去自定義filed,也就是自己的模型。比如上篇的用戶(hù)交互系統(tǒng),我們就不僅繼承的原有MyUser,還自定義了消息的模型:online_message以及系統(tǒng)的狀態(tài)模型:online_SystemStatus.
2.2 創(chuàng)建步驟:
2.2.1.創(chuàng)建django項(xiàng)目:
django-admin startproject test
2.2.2.創(chuàng)建webapp:
cd test
django-admin startapp sql
2.2.3.添加webapp進(jìn)test項(xiàng)目:
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.定義sql的數(shù)據(jù)模型:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=30)
age = models.IntegerField()
2.2.5.初始化數(shù)據(jù)庫(kù)數(shù)據(jù):
cd ~/test
python manage.py makemigraTIons sql
python manage.py migrate
3.實(shí)例模型代碼:
#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數(shù)據(jù) 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)
評(píng)論