跟着老师用Navicat Data Modeler画了一下大概的数据模型
主要有User,Tag,Category,ArticleManager,Article,Comment,Links,Ad这几张表。
Navicat Data Modeler这个软件给我的最大感觉是小巧,使用方便,但是功能缺失有局限,只适合用于小的项目(个人感觉^ ^,不喜勿喷)。
根据pdm在blog/models.py中定义模型
#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import AbstractUser
from django.db import models
# Create your models here.
# 用户模型.
# 第一种:采用的继承方式扩展用户信息(本系统采用)
# 扩展:关联的方式去扩展用户信息
class User(AbstractUser):
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号码')
mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
class Meta:
verbose_name = '用户'
verbose_name_plural = verbose_name
ordering = ['-id']
def __unicode__(self):
return self.username
# tag(标签)
class Tag(models.Model):
name = models.CharField(max_length=30, verbose_name='标签名称')
class Meta:
verbose_name = '标签'
verbose_name_plural = verbose_name
def __unicode__(self):
return self.name
# 分类
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
# 自定义一个文章Model的管理器
# 1、新加一个数据处理的方法
# 2、改变原有的queryset
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
# 文章模型
class Article(models.Model):
title = models.CharField(max_length=50, verbose_name='文章标题')
desc = models.CharField(max_length=50, verbose_name='文章描述')
content = models.TextField(verbose_name='文章内容')
click_count = models.IntegerField(default=0, verbose_name='点击次数')
is_recommend = models.BooleanField(default=False, verbose_name='是否推荐')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
user = models.ForeignKey(User, verbose_name='用户')
category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分类')
tag = models.ManyToManyField(Tag, verbose_name='标签')
objects = ArticleManager()
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
ordering = ['-date_publish']
def __unicode__(self):
return self.title
# 评论模型
class Comment(models.Model):
content = models.TextField(verbose_name='评论内容')
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='个人网页地址')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
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='父级评论')
class Meta:
verbose_name = '评论'
verbose_name_plural = verbose_name
def __unicode__(self):
return str(self.id)
# 友情链接
class Links(models.Model):
title = models.CharField(max_length=50, verbose_name='标题')
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='发布时间')
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
# 广告
class Ad(models.Model):
title = models.CharField(max_length=50, verbose_name='广告标题')
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='回调url')
date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
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
下载安装mysql,下载的mysql-community-server版,为免安装版。
设置参考百度经验……
root用户的默认密码为空,不需要输入,更改密码是用
>>>mysqladmin -u root password "root"
修改默认的sqlite3数据库为mysql数据库,修改settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog_db',
'HOST':'127.0.0.1',
'PORT':3306,
'USER':'root',
'PASSWORD':'root',
}
}
由于引用django.contrib.auth.models.AbstractUser
AUTH_USER_MODEL = 'blog.User'
pycharm中Tools>run manage.py Task 中运行
makemigrations#生产sql
migrate #执行sql,生成对应的表
生成如下列表:
Paste_Image.pngcreatesuperuser添加超级账户
如遇数据库模型变更,则更新数据库,使用以下语句:
makemigrations
syncdb
网友评论