博客案例
数据库设计
from django.db import models # Create your models here. # 数据库设计: # 用户表: uid,username,pwd,email,img, class UserInfo(models.Model): '''用户表''' nid = models.AutoField(primary_key=True) username = models.CharField(verbose_name='用户名',max_length=32,unique=True) pwd = models.CharField(verbose_name='密码',max_length=64) nickname = models.CharField(verbose_name='昵称',max_length=32) email = models.EmailField(verbose_name='邮箱',unique=True) avatar = models.ImageField(verbose_name='头像') create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True) fans = models.ManyToManyField(verbose_name='粉丝们', to='UserInfo', through='UserFans', related_name='f', through_fields=('user','follower')) #博客表: bid,surfix,theme,title,summary, FK(用户表,unique)=OneToOne(用户表) class Blog(models.Model): """ 博客信息 """ nid = models.BigAutoField(primary_key=True) title = models.CharField(verbose_name='个人博客标题',max_length=64) site = models.CharField(verbose_name='个人博客前缀',max_length=32,unique=True) theme = models.CharField(verbose_name='博客主题',max_length=32) user = models.OneToOneField(to='UserInfo',to_field='nid') # 互粉表: id 明星ID(用户表) 粉丝ID(用户表) class UserFans(models.Model): """ 互粉关系表 """ user = models.ForeignKey(verbose_name='博主',to='UserInfo',to_field='nid',related_name='users') follower = models.ForeignKey(verbose_name='粉丝',to='UserInfo',to_field='nid',related_name='followers') class Meta: unique_together = [ ('user','follower'), ] # 分类表:caption Fk(博客bid) 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') # 文章:id,title,summary,ctime,FK(个人分类表),主站分类(choices) class Article(models.Model): nid = models.BigAutoField(primary_key=True) title = models.CharField(verbose_name='文章标题', max_length=128) summary = models.CharField(verbose_name='文章简介', max_length=255) read_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) up_count = models.IntegerField(default=0) down_count = models.IntegerField(default=0) create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid') category = models.ForeignKey(verbose_name='文章类型', to='Category', to_field='nid', null=True) type_choices = [ (1, "Python"), (2, "Linux"), (3, "OpenStack"), (4, "GoLang"), ] article_type_id = models.IntegerField(choices=type_choices, default=None) tags = models.ManyToManyField( to="Tag", through='Article2Tag', through_fields=('article', 'tag'), ) # 文章详细:detail OneToOne(文章) class ArticleDetail(models.Model): content = models.TextField('文章内容') article = models.OneToOneField(verbose_name='所属文章',to='Article',to_field='nid') # 标签表:caption Fk(博客bid) 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') # 文章标签关系: 文章ID 标签ID class Article2Tag(models.Model): article = models.ForeignKey(verbose_name='文章',to='Article',to_field='nid') tag = models.ForeignKey(verbose_name='标签',to='Tag',to_field='nid') class Meta: unique_together = [ ('article','tag'), ] # 赞踩文章关系: 文章ID 用户ID 赞或踩(True,False) 联合唯一索引:(文章ID 用户ID ) class UpDown(models.Model): """ 文章顶或踩 """ article = models.ForeignKey(verbose_name='文章',to='Article',to_field='nid') user = models.ForeignKey(verbose_name='赞或踩用户',to='UserInfo',to_field='nid') up = models.BooleanField(verbose_name='是否赞') class Meta: unique_together = [ ('article', 'user'), ] # 评论表:id,content,FK(文章),FK(user),ctime,parent_comment_id class Comment(models.Model): """ 评论表 """ nid = models.BigAutoField(primary_key=True) content = models.CharField(verbose_name='评论内容',max_length=255) create_time = models.DateTimeField(verbose_name='创建时间',auto_now_add=True) reply = models.ForeignKey(verbose_name='回复评论',to='self',related_name='back',null=True) article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid') user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid')