博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BBS-项目流程分析-表的创建
阅读量:5369 次
发布时间:2019-06-15

本文共 13899 字,大约阅读时间需要 46 分钟。

http://www.cnblogs.com/alice-bj/tag/Django%20-%20BBS/

https://www.cnblogs.com/venicid/p/9365019.html#_label0

1、项目流程

项目流程:1 搞清楚需求(产品经理)  (1) 基于用户认证组件和Ajax实现登录验证(图片验证码)  (2) 基于forms组件和Ajax实现注册功能  (3) 设计系统首页(文章列表渲染)  (4) 设计个人站点页面---跨表查询,分组查询  (5) 文章详情页  (6) 实现文章点赞功能  (7) 实现文章的评论      ---文章的评论      ---评论的评论  (8) 富文本编辑框 和 防止xss攻击(防止别人提交js代码)2 设计表结构3 按着每一个功能分别进行开发  4 功能测试5 项目部署上线

2、设计表结构

关联字段放在多的一方

2 设计表结构            from django.contrib.auth.models import User,AbstractUser            class UserInfo(AbstractUser):                """                用户信息                """                nid = models.AutoField(primary_key=True)                telephone = models.CharField(max_length=11, null=True, unique=True)                avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png")                create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)                blog = models.OneToOneField(to='Blog', to_field='nid', null=True,on_delete=models.CASCADE)                def __str__(self):                    return self.username            class Blog(models.Model):                """                博客信息表(站点表)                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='个人博客标题', max_length=64)                site_name = models.CharField(verbose_name='站点名称', max_length=64)                theme = models.CharField(verbose_name='博客主题', max_length=32)                def __str__(self):                    return self.title            class Category(models.Model):                """                博主个人文章分类表                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='分类标题', max_length=32)                blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title            class Tag(models.Model):                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='标签名称', max_length=32)                blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title            class Article(models.Model):                nid = models.AutoField(primary_key=True)                title = models.CharField(max_length=50, verbose_name='文章标题')                desc = models.CharField(max_length=255, verbose_name='文章描述')                create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)                content = models.TextField()                comment_count=models.IntegerField(default=0)                up_count=models.IntegerField(default=0)                down_count=models.IntegerField(default=0)                user = models.ForeignKey(verbose_name='作者', to='UserInfo', to_field='nid',on_delete=models.CASCADE)                category = models.ForeignKey(to='Category', to_field='nid', null=True,on_delete=models.CASCADE)                tags = models.ManyToManyField(                    to="Tag",                    through='Article2Tag',                    through_fields=('article', 'tag'),                )                def __str__(self):                    return self.title            class Article2Tag(models.Model):                nid = models.AutoField(primary_key=True)                article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid',on_delete=models.CASCADE)                tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid',on_delete=models.CASCADE)                class Meta:                    unique_together = [                        ('article', 'tag'),                    ]                def __str__(self):                    v = self.article.title + "---" + self.tag.title                    return v            class ArticleUpDown(models.Model):                """                点赞表                """                nid = models.AutoField(primary_key=True)                user = models.ForeignKey('UserInfo', null=True,on_delete=models.CASCADE)                article = models.ForeignKey("Article", null=True,on_delete=models.CASCADE)                is_up = models.BooleanField(default=True)                class Meta:                    unique_together = [                        ('article', 'user'),                    ]            class Comment(models.Model):                """                评论表                """                nid = models.AutoField(primary_key=True)                 user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid',on_delete=models.CASCADE)                article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid',on_delete=models.CASCADE)                 create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)                content = models.CharField(verbose_name='评论内容', max_length=255)                parent_comment=models.ForeignKey("self",null=True,on_delete=models.CASCADE)                def __str__(self):                    return self.content              根评论:对文章的评论                        子评论:对评论的评论                        111                444                   555            222            333            Comment            nid   user_id  article_id    content      parent_comment_id(null=True)             1      1          1            111              null             2      2          1            222              null             3      3          1            333              null             4      4          1            444                1              5      5          1            555                4

对表逻辑分析

 

(1)用户表分析

UserInfo继承AbstractUser类

之前的用户认证组件的User表是继承AbstractUser类,并且不能添加字段

 
from django.contrib.auth.models import User,AbstractUser
class UserInfo(AbstractUser):                """                用户信息                """                nid = models.AutoField(primary_key=True)                telephone = models.CharField(max_length=11, null=True, unique=True)                avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png")                create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)           可以扩展其他的字段:name 、age等等                blog = models.OneToOneField(to='Blog', to_field='nid', null=True,on_delete=models.CASCADE)                def __str__(self):                    return self.username

博客信息表:

class Blog(models.Model):                """                博客信息表(站点表)                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='个人博客标题', max_length=64)                site_name = models.CharField(verbose_name='站点名称', max_length=64)                theme = models.CharField(verbose_name='博客主题', max_length=32)                def __str__(self):                    return self.title 用户的信息字段

分类表

class Category(models.Model):                """                博主个人文章分类表                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='分类标题', max_length=32)                blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title

一个标签属于多个文章,一个文章有多个标签

分类和站点

站点和用户一对一

用户和分类一对多

 标签表

class Tag(models.Model):                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='标签名称', max_length=32)                 blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title

 评论表

class Comment(models.Model):                """                评论表                """                nid = models.AutoField(primary_key=True)                user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid',on_delete=models.CASCADE)                article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid',on_delete=models.CASCADE)                create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)                content = models.CharField(verbose_name='评论内容', max_length=255)                parent_comment=models.ForeignKey("self",null=True,on_delete=models.CASCADE)#自关联,用于建立子评论                def __str__(self):                    return self.content

 博客系统的创建项目与数据迁移

 

在models.py

from django.contrib.auth.models import User,AbstractUser            class UserInfo(AbstractUser):                """                用户信息                """                nid = models.AutoField(primary_key=True)                telephone = models.CharField(max_length=11, null=True, unique=True)                avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png")                create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)                blog = models.OneToOneField(to='Blog', to_field='nid', null=True,on_delete=models.CASCADE)                def __str__(self):                    return self.username            class Blog(models.Model):                """                博客信息表(站点表)                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='个人博客标题', max_length=64)                site_name = models.CharField(verbose_name='站点名称', max_length=64)                theme = models.CharField(verbose_name='博客主题', max_length=32)                def __str__(self):                    return self.title            class Category(models.Model):                """                博主个人文章分类表                """                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='分类标题', max_length=32)                blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title            class Tag(models.Model):                nid = models.AutoField(primary_key=True)                title = models.CharField(verbose_name='标签名称', max_length=32)                blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid',on_delete=models.CASCADE)                def __str__(self):                    return self.title            class Article(models.Model):                nid = models.AutoField(primary_key=True)                title = models.CharField(max_length=50, verbose_name='文章标题')                desc = models.CharField(max_length=255, verbose_name='文章描述')                create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)                content = models.TextField()                comment_count=models.IntegerField(default=0)                up_count=models.IntegerField(default=0)                down_count=models.IntegerField(default=0)                user = models.ForeignKey(verbose_name='作者', to='UserInfo', to_field='nid',on_delete=models.CASCADE)                category = models.ForeignKey(to='Category', to_field='nid', null=True,on_delete=models.CASCADE)                tags = models.ManyToManyField(                    to="Tag",                    through='Article2Tag',                    through_fields=('article', 'tag'),                )                def __str__(self):                    return self.title            class Article2Tag(models.Model):                nid = models.AutoField(primary_key=True)                article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid',on_delete=models.CASCADE)                tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid',on_delete=models.CASCADE)                class Meta:                    unique_together = [                        ('article', 'tag'),                    ]                def __str__(self):                    v = self.article.title + "---" + self.tag.title                    return v            class ArticleUpDown(models.Model):                """                点赞表                """                nid = models.AutoField(primary_key=True)                user = models.ForeignKey('UserInfo', null=True,on_delete=models.CASCADE)                article = models.ForeignKey("Article", null=True,on_delete=models.CASCADE)                is_up = models.BooleanField(default=True)                class Meta:                    unique_together = [                        ('article', 'user'),                    ]            class Comment(models.Model):                """                评论表                """                nid = models.AutoField(primary_key=True)                user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid',on_delete=models.CASCADE)                article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid',on_delete=models.CASCADE)                create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)                content = models.CharField(verbose_name='评论内容', max_length=255)                parent_comment=models.ForeignKey("self",null=True,on_delete=models.CASCADE)                def __str__(self):                    return self.content
models

连接mysql数据库

开启数据库

settings.py

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME':'cnblog',           # 要连接的数据库,连接前需要创建好        'USER':'root',        # 连接数据库的用户名        'PASSWORD':'123',        # 连接数据库的密码        'HOST':'127.0.0.1',       # 连接主机,默认本级        'PORT':3306            #  端口 默认3306    }}
AUTH_USER_MODEL='blog.UserInfo'
 

 

在项目名下的init添加如下代码

import pymysqlpymysql.install_as_MySQLdb()

在pycharm的Terminal执行

python manage.py makemigrations

python manage.py migrate

mysql生成表

 

在pycharm中查看生成的表

 

 

 

转载于:https://www.cnblogs.com/foremostxl/p/9985044.html

你可能感兴趣的文章
《演说之禅》I & II 读书笔记
查看>>
thinkphp3.2接入支付宝支付接口(PC端)
查看>>
【转】在Eclipse中安装和使用TFS插件
查看>>
C#中Monitor和Lock以及区别
查看>>
【NOIP2017】奶酪
查看>>
5.6.3.7 localeCompare() 方法
查看>>
Linux下好用的简单实用命令
查看>>
描绘应用程序级的信息
查看>>
php环境搭建脚本
查看>>
php 编译常见错误
查看>>
MES架构
查看>>
hdu 2767(tarjan)
查看>>
sklearn之分类模型混淆矩阵和分类报告
查看>>
MySQL各存储引擎
查看>>
项目--简单导出CSV文件
查看>>
Oracle session相关数据字典(一)
查看>>
C#用正则表达式 获取网页源代码标签的属性或值
查看>>
BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡(贪心)
查看>>
WCF(一) 简单的认知
查看>>
[MFC][DShow]简单例子
查看>>