一.前言
在经过上篇《Dragonboard410c 服务器系列六之用户交互系统》的学习,博主相信大家都能大致掌握Django框架基础的web服务器搭建以及简单的数据交互。但是,往深处想,Django是如何存储不同页面的交互数据的?相信有看过博主之前上传的代码的童鞋都已经知晓,这也是我们今天要讨论的主题:数据库。
二.数据库
1.概念:
数据库(Database)是按照数据结构来组织、存储和管理数据的建立在计算机存储设备上的仓库。简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进行新增、截取、更新、删除等操作。在经济管理的日常工作中,常常需要把某些相关的数据放进这样的“仓库”,并根据管理的需要进行相应的处理。可以说数据库的设计是web后台架构设计的重中之重,合理的数据库设计将会为后台节省大量的运算步骤和提高整体的运行效率,这也是衡量一个后台工程师功底的重要标准。
2.Django的数据库:
2.1.介绍:
Django的数据库,我们称之为模型,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.py中配置即可,不用更改models.py中的代码,丰富的API极大的方便了使用。当然,我们也可以根据自己的需要去自定义filed,也就是自己的模型。比如上篇的用户交互系统,我们就不仅继承的原有MyUser,还自定义了消息的模型:online_message以及系统的状态模型:online_SystemStatus.
2.2创建步骤:
2.2.1.创建django项目:
django-admin startproject test
2.2.2.创建webapp:
cd test
django-admin startapp sql
2.2.3.添加webapp进test项目:
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的数据模型:
from
django.db
import
models
class
Person(models.Model):
name
=
models.CharField(max_length
=
30
)
age
=
models.IntegerField()
2.2.5.初始化数据库数据:
cd ~/test
python manage.py makemigrations sql
python manage.py migrate
3.实例模型代码:
#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数据 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) #用户独有的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): #实例化对象: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