解析Dragonboard 410c搭載web服務(wù)器之?dāng)?shù)據(jù)庫的設(shè)計(jì)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
一.前言
在經(jīng)過上篇《Dragonboard410c 服務(wù)器系列六之用戶交互系統(tǒng)》的學(xué)習(xí),博主相信大家都能大致掌握Django框架基礎(chǔ)的web服務(wù)器搭建以及簡單的數(shù)據(jù)交互。但是,往深處想,Django是如何存儲不同頁面的交互數(shù)據(jù)的?相信有看過博主之前上傳的代碼的童鞋都已經(jīng)知曉,這也是我們今天要討論的主題:數(shù)據(jù)庫。
二.數(shù)據(jù)庫
1.概念:
數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織、存儲和管理數(shù)據(jù)的建立在計(jì)算機(jī)存儲設(shè)備上的倉庫。簡單來說是本身可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數(shù)據(jù)進(jìn)行新增、截取、更新、刪除等操作。在經(jīng)濟(jì)管理的日常工作中,常常需要把某些相關(guān)的數(shù)據(jù)放進(jìn)這樣的“倉庫”,并根據(jù)管理的需要進(jìn)行相應(yīng)的處理??梢哉f數(shù)據(jù)庫的設(shè)計(jì)是web后臺架構(gòu)設(shè)計(jì)的重中之重,合理的數(shù)據(jù)庫設(shè)計(jì)將會為后臺節(jié)省大量的運(yùn)算步驟和提高整體的運(yùn)行效率,這也是衡量一個(gè)后臺工程師功底的重要標(biāo)準(zhǔn)。
2.Django的數(shù)據(jù)庫:
2.1.介紹:
Django的數(shù)據(jù)庫,我們稱之為模型,與數(shù)據(jù)庫相關(guān)的代碼一般寫在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等數(shù)據(jù)庫,只需要在setTIngs.py中配置即可,不用更改models.py中的代碼,豐富的API極大的方便了使用。當(dāng)然,我們也可以根據(jù)自己的需要去自定義filed,也就是自己的模型。比如上篇的用戶交互系統(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ù)庫數(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)[!--empirenews.page--]
#text=models.CharField(max_length=254, unique=True, db_index=True)
is_staff = models.BooleanField('staff status', default=False)
# userID = models.IntegerField(default=1) #用戶獨(dú)有的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
#建立推送消息的模型
class Message(models.Model):
# username = models.CharField(max_length=254) #消息推送者的名字
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):
# 在Python3中使用 def __str__(self)
return self.name
class SystemStatus(models.Model): #實(shí)例化對象: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):
# 在Python3中使用 def __str__(self)
return self.name
三.例程分享:http://pan.baidu.com/share/link?shareid=3536829173&uk=1812979481