1 知識(shí)點(diǎn)
主要是分析設(shè)計(jì)數(shù)據(jù)庫(kù)的數(shù)據(jù)表和數(shù)據(jù)表字段苍鲜,然后使用Navicat Data Modeler創(chuàng)建模
將sqlite數(shù)據(jù)庫(kù)修改成mysql數(shù)據(jù)庫(kù)缅帘,同步數(shù)據(jù)
2 模型
2.1 數(shù)據(jù)表所有的數(shù)據(jù)列以及其對(duì)應(yīng)的數(shù)據(jù)類型和約束
- nlog_user用戶表【id皆刺、password(密碼)、last_login(上次一登錄時(shí)間)葱蝗、is_superuser(是否管理員)剥扣、username(用戶名)、firstname(姓)适滓、lastname(名)敦迄、email(郵箱)、is_staff()、is_active()罚屋、date_joined(創(chuàng)建時(shí)間)苦囱、avatar(頭像)、qq(QQ號(hào)碼)脾猛、mobile(手機(jī)號(hào)碼)撕彤、url(個(gè)人博客地址)】
- blog_tag標(biāo)簽表【id、tag(標(biāo)簽名)】
- blog_category分類表【id猛拴、name(分類名稱)羹铅、index(分類排序)】
- blog_article文章表【id、title(文章標(biāo)題)愉昆、desc(文章描述)职员、content(文章內(nèi)容)、ckick_count(點(diǎn)擊次數(shù))撼唾、is_recommend(是否推薦)廉邑、date_publish(發(fā)布時(shí)間)、user(用戶)倒谷、category(分類)、tag(標(biāo)簽)】
- blog_comment評(píng)論表【id糙箍、content(評(píng)論內(nèi)容)渤愁、username(用戶名)、email(郵箱地址)深夯、url(個(gè)人網(wǎng)頁(yè)地址)抖格、date_publish(發(fā)布時(shí)間)、user(用戶)咕晋、article(文章)雹拄、pid(父級(jí)評(píng)論)】
- blog_links友情鏈接【id、title(標(biāo)題)掌呜、description(友情鏈接描述)滓玖、callback_url(url地址)、date_publish(發(fā)布時(shí)間)质蕉、index(排列順序)】
- blog_ad廣告【id势篡、title(廣告標(biāo)題)、description(廣告描述)模暗、image_url(圖片路徑)禁悠、callback_url(回調(diào)url)、date_publish(發(fā)布時(shí)間)兑宇、index(排列順序)】
2.2 設(shè)計(jì)數(shù)據(jù)模型圖
- 紅色部分需要寫Moudels設(shè)計(jì)生成
- 綠色部分繼承AbstractUser生成
- 其中橙色blog_article_tag由多對(duì)多生成
3 Models設(shè)計(jì)
3.1 數(shù)據(jù)庫(kù)修改
將sqlite修改成mysql【settings.py】
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
# 將數(shù)據(jù)庫(kù)改成mysql數(shù)據(jù)庫(kù)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blogdb',
'USER': 'root',
'PASSWORD': 'root',
# 'HOST': '',
# 'PORT': '',
}
}
由于本地環(huán)境問題碍侦,Python_mysql無法安裝,所以使用pymysql【__init__.py】
import pymysql
pymysql.install_as_MySQLdb()
3.2 創(chuàng)建Models
根據(jù)自己設(shè)計(jì)的模型,創(chuàng)建相關(guān)的Model【Models.py】
# coding: utf-8
# author: spareribs
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
'''
############ 本地模型用到的字段類型和和參數(shù)的含義
max_length:最大長(zhǎng)度
blank:True可以為空
null:可以為null
verbose_name:admin顯示名稱
default:默認(rèn)值
unique:True表示唯一
【ImageField】:upload_to表示MEDIA_ROOT的子目錄瓷产,用來存放上傳的文件
【CharField】
【URLField】
【IntegerField】
【DateTimeField】:auto_now_add表示自動(dòng)設(shè)置當(dāng)前時(shí)間
【TextField】
【BooleanField】
【EmailField】
'''
# 用戶(User)模型
# 采用的繼承方式擴(kuò)展用戶信息
class User(AbstractUser):
# 在繼承的基礎(chǔ)上新增4個(gè)字段
avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.png', max_length=200, blank=True,
null=True, verbose_name='用戶頭像')
qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ號(hào)碼')
mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手機(jī)號(hào)碼')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='個(gè)人網(wǎng)頁(yè)地址')
# 使用內(nèi)部的class Meta 定義模型的元數(shù)據(jù)
class Meta:
# verbose_name:數(shù)據(jù)庫(kù)表名名稱比规,這里表名稱為“用戶”
verbose_name = '用戶'
# verbose_name_plural:人類可讀的單復(fù)數(shù)名稱,這里“用戶”復(fù)數(shù)名稱為“用戶”
verbose_name_plural = verbose_name
# ordering:如排序選項(xiàng)拦英,這里以id降序來排序
ordering = ['-id']
# 對(duì)象的字符串表達(dá)式(unicode格式)
def __unicode__(self):
return self.username
# 標(biāo)簽(tag)模型
class Tag(models.Model):
name = models.CharField(max_length=30, verbose_name='標(biāo)簽名稱')
class Meta:
verbose_name = '標(biāo)簽'
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
# 分類(category)模型
class Category(models.Model):
name = models.CharField(max_length=30, verbose_name='分類名稱')
index = models.IntegerField(default=999, verbose_name='分類的排序')
class Meta:
verbose_name = '分類'
verbose_name_plural = verbose_name
ordering = ['index', 'id']
def __unicode__(self):
return self.name
# 自定義一個(gè)文章Model的管理器
# 1蜒什、新加一個(gè)數(shù)據(jù)處理的方法
# 2、改變?cè)械膓ueryset
class ArticleManager(models.Manager):
def distinct_date(self):
distinct_date_list = []
date_list = self.values('date_publish')
for date in date_list:
date = date['date_publish'].strftime('%Y/%m文章存檔')
if date not in distinct_date_list:
distinct_date_list.append(date)
return distinct_date_list
# 文章(aticle)模型
class Article(models.Model):
title = models.CharField(max_length=50, verbose_name='文章標(biāo)題')
desc = models.CharField(max_length=50, verbose_name='文章描述')
content = models.TextField(verbose_name='文章內(nèi)容')
click_count = models.IntegerField(default=0, verbose_name='點(diǎn)擊次數(shù)')
is_recommend = models.BooleanField(default=False, verbose_name='是否推薦')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='發(fā)布時(shí)間')
#
user = models.ForeignKey(User, verbose_name='用戶')
category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分類')
tag = models.ManyToManyField(Tag, verbose_name='標(biāo)簽')
objects = ArticleManager()
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
ordering = ['-date_publish']
def __unicode__(self):
return self.title
# 評(píng)論(comment)模型
class Comment(models.Model):
content = models.TextField(verbose_name='評(píng)論內(nèi)容')
username = models.CharField(max_length=30, blank=True, null=True, verbose_name='用戶名')
email = models.EmailField(max_length=50, blank=True, null=True, verbose_name='郵箱地址')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='個(gè)人網(wǎng)頁(yè)地址')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='發(fā)布時(shí)間')
#
user = models.ForeignKey(User, blank=True, null=True, verbose_name='用戶')
article = models.ForeignKey(Article, blank=True, null=True, verbose_name='文章')
pid = models.ForeignKey('self', blank=True, null=True, verbose_name='父級(jí)評(píng)論')
class Meta:
verbose_name = '評(píng)論'
verbose_name_plural = verbose_name
def __unicode__(self):
return str(self.id)
# 友情鏈接(links)模型
class Links(models.Model):
title = models.CharField(max_length=50, verbose_name='標(biāo)題')
description = models.CharField(max_length=200, verbose_name='友情鏈接描述')
callback_url = models.URLField(verbose_name='url地址')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='發(fā)布時(shí)間')
index = models.IntegerField(default=999, verbose_name='排列順序(從小到大)')
class Meta:
verbose_name = '友情鏈接'
verbose_name_plural = verbose_name
ordering = ['index', 'id']
def __unicode__(self):
return self.title
# 廣告(ad)模型
class Ad(models.Model):
title = models.CharField(max_length=50, verbose_name='廣告標(biāo)題')
description = models.CharField(max_length=200, verbose_name='廣告描述')
image_url = models.ImageField(upload_to='ad/%Y/%m', verbose_name='圖片路徑')
callback_url = models.URLField(null=True, blank=True, verbose_name='回調(diào)url')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='發(fā)布時(shí)間')
index = models.IntegerField(default=999, verbose_name='排列順序(從小到大)')
class Meta:
verbose_name = u'廣告'
verbose_name_plural = verbose_name
ordering = ['index', 'id']
def __unicode__(self):
return self.title
使用用戶自定義的User Model【settings.py】
# 自定義用戶Model
AUTH_USER_MODEL = 'blog.User'
3.3 創(chuàng)建數(shù)據(jù)庫(kù)并同步數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)庫(kù)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blogdb;
Query OK, 1 row affected (0.00 sec)
同步
(env_py27_django_blog) D:\MaiZi_Edu\Dropbox\Maizi\Django_blog\django_blog>python manage.py makemigrations
2017-01-02 12:41:28,928 [MainThread:1008] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
Migrations for 'blog':
0001_initial.py:
- Create model User
- Create model Ad
- Create model Article
- Create model Category
- Create model Comment
- Create model Links
- Create model Tag
- Add field category to article
- Add field tag to article
- Add field user to article
(env_py27_django_blog) D:\MaiZi_Edu\Dropbox\Maizi\Django_blog\django_blog>python manage.py migrate
2017-01-02 12:41:35,924 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.001) SET SQL_AUTO_IS_NULL = 0; args=None
2017-01-02 12:41:35,980 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
2017-01-02 12:41:35,986 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.001) SHOW FULL TABLES; args=None
2017-01-02 12:41:35,992 [MainThread:8876] [django.db.backends.schema:102] [schema:execute] [DEBUG]- CREATE TABLE `django_migrations` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `app` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `applied` datetime(6) NOT NULL); (params None)
2017-01-02 12:41:36,085 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.086) CREATE TABLE `django_migrations` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `app` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `applied` datetime(6) NOT NULL); args=None
2017-01-02 12:41:36,119 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.025) SELECT engine FROM information_schema.tables WHERE table_name = 'django_migrations'; args=[u'django_migrations']
2017-01-02 12:41:36,144 [MainThread:8876] [django.db.backends:89] [utils:execute] [DEBUG]- (0.002) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()
...
...
(此處省略很多內(nèi)容)
查看驗(yàn)證數(shù)據(jù)庫(kù)是否已經(jīng)創(chuàng)建成功
4 拓展
- 繼承的方式拓展用戶信息
- 關(guān)聯(lián)的方式拓展用戶信息
相關(guān)下載
數(shù)據(jù)庫(kù)設(shè)計(jì)_代碼
歡迎留言疤估,博文會(huì)持續(xù)更新~~