blog建表操作
表思维导图:
数据库:表
from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserInfo(AbstractUser): #settings :AUTH_USER_MODEL ="项目名称.UserInfo"
'''用户信息表'''
nid = models.BigAutoField(primary_key=True)
nickname =models.CharField(max_length=32,verbose_name="昵称",unique=True)
tel = models.IntegerField(verbose_name="电话",unique=True,null=True,blank=True)
email = models.CharField(max_length=64,verbose_name="邮箱")
avatar = models.FileField(verbose_name="头像",upload_to="avatar",default="/avatar/default.png")
create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)
class Meta:
verbose_name_plural = "用户信息表"
def __str__(self):
return self.username
class Article(models.Model):
'''
文章表
'''
title = models.CharField(max_length=64,verbose_name="文章标题")
summary = models.CharField(max_length=244, verbose_name="文章概要")
create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)
update_time = models.DateTimeField(verbose_name="修改时间",auto_now=True)
poll_count = models.IntegerField(verbose_name="点赞数",default=0)
comment_count = models.IntegerField(verbose_name="评论数",default=0)
read_count = models.IntegerField(verbose_name="阅读数",default=0)
# is_essence = models.BooleanField(verbose_name="是否精华",default=0)
# is_top = models.BooleanField(verbose_name="是否置顶",default=0)
user = models.ForeignKey(to="UserInfo",verbose_name="所属作者",null=True,blank=True)
classify = models.ForeignKey(to="Classfication",verbose_name="所属类别",null=True,blank=True)
tags = models.ManyToManyField(to="Tag",through="Article2tag",through_fields=('article', 'tag'),verbose_name="所属标签")
type_choices = [
(1,"编程语言"),
(2,"软件设计"),
(3,"前端系列"),
(4,"数据库"),
(5,"操作系统")
]
artcle_type_id = models.IntegerField(choices=type_choices,default=None)
class Meta:
verbose_name_plural = "文章表"
def __str__(self):
return self.title
class Article_detail(models.Model):
'''文章细节表'''
article = models.OneToOneField(to="Article",verbose_name="所属文章")
content =models.TextField(max_length=400,verbose_name="文章内容")
class Meta:
verbose_name_plural = "文章细节表"
class Tag(models.Model):
'''标签表'''
name = models.CharField(max_length=32,verbose_name="标签名")
blog = models.ForeignKey(to="Blog",verbose_name="所属博客")
class Meta:
verbose_name_plural = "标签表"
class Article2tag(models.Model):
article = models.ForeignKey(verbose_name="文章",to="Article")
tag = models.ForeignKey(verbose_name="标签",to="Tag")
class Meta:
'''联合唯一'''
unique_together = [
("article","tag")
]
class Comment(models.Model):
'''评论表'''
time = models.DateTimeField(verbose_name="评论时间",auto_now_add=True)
content = models.CharField(max_length=265,verbose_name="评论内容")
up_count = models.IntegerField(default=0)
user = models.ForeignKey(to="UserInfo",verbose_name="评论人",null=True,blank=True)
article = models.ForeignKey(to="Article",verbose_name="评论文章",null=True,blank=True)
farther_comment = models.ForeignKey(to="Comment",verbose_name="父级评论",null=True,blank=True)
# farther_comment = models.ForeignKey("self",verbose_name="父级评论",null=True,blank=True)
class Meta:
verbose_name_plural = "评论表"
class Article_poll(models.Model):
'''文章点赞表'''
time = models.DateTimeField(verbose_name="点赞时间",auto_now_add=True)
article = models.ForeignKey(to="Article",verbose_name="点赞文章",null=True,blank=True) #一个文章可以有多个赞
user = models.ForeignKey(to="UserInfo",verbose_name="点赞人",null=True,blank=True)
# is_positive = models.BooleanField(default=1,verbose_name="点赞或踩")
class Meta:
'''联合唯一'''
unique_together = ("user", "article",)
verbose_name_plural = "文章点赞表"
class Comment_poll(models.Model):
'''评论点赞表'''
time=models.DateTimeField(verbose_name="点赞时间",auto_now_add=True)
# is_positive = models.BooleanField(verbose_name="点赞或踩",default=1)
user = models.ForeignKey(to="UserInfo",verbose_name="点赞用户",null=True,blank=True)
comment = models.ForeignKey(to="Comment",verbose_name="点赞所属评论",null=True,blank=True) #一个评论可以有多个赞
class Meta:
'''联合唯一'''
unique_together = ("user","comment",)
verbose_name_plural = "评论点赞表"
class Blog(models.Model):
'''个人站点表'''
title = models.CharField(max_length=32,verbose_name="个人博客标题")
url = models.CharField(max_length=64,verbose_name="路径",unique=True)
theme = models.CharField(max_length=32,verbose_name="博客主题")
user = models.OneToOneField(to="UserInfo", verbose_name="所属用户")
class Meta:
'''通过admin录入数据的时候个中文显示'''
verbose_name_plural = "个人站点表"
def __str__(self):
return self.title
class Classfication(models.Model):
'''博主个人文章分类表'''
title = models.CharField(max_length=32, verbose_name="分类标题")
blog = models.ForeignKey(to="Blog",verbose_name="所属博客")
class Meta:
verbose_name_plural = "分类表"
注释:
十张表: 用户表(userinfo): nickname(用户昵称) telephone(手机号) avatar(头像) create_time(创建时间) 站点表(Blog): title(博客标题) url(路径) theme(博客主题) user和用户表关系(one2one) 文章表(Article): title(标题) desc(内容) read_count(文章阅读量) comment_count(文章评论量) up_count(文章置顶) down_count(文章置末尾) create_time(文章创建时间) category(类型)和分类表(fk) user(所属用户)和用户表(fk) tags(标签)和标签表(m2m) 文章详情表(ArticleDetail): content(文章类容) article(所属文章)和文章表示(one2one)
评论表(Comment):
content(评论语)
create_time(创建时间)
up_count(评论量)
user(评论者)和用户(fk)
article(评论文章)和文章表(fk)
parent_comment(父级评论)自关联
评论点赞表(CommentUp):
user(点赞的用户)和用户表(fk)
comment(点赞评论)和评论表(fk)
文章点赞表(ArticleUp):
user(点赞的用户)和用户表(fk)
comment(被点赞文章)和文章表(fk)
标签表(Tag):
title(标签名称)
bolg(所属博客)和博客表(fk)
文章和标签表(Article2Tag):
article(文章)和文章表(fk)
tag(标签)和标签表(fk)