一、首先理清各表之间的关系:
二、依照上图的关系建立models:
User用户表:(继承django自带的用户表,在配置文件中务必加)
User用户表:(继承django自带的用户表,在配置文件中务必加)
字段:
nid(编号)
nickname(昵称)
telephone(手机号)
avatar(头像)
create_time(创建时间)
Blog博主个人站点表: 字段: title(标题) url(url后缀) theme(个人博客主题) user(对应用户)---------和用户表一对一 Classfication文章分类表: 字段: title(类别名) blog(所属博客)---------和博客表多对一
Article 文章表: 字段: title(标题) summary(文章摘要) read_count(文章阅读量) comment_count(文章评论数量) create_time(文章创建时间) classify(文章类型) 和类型表多对一 user(所属用户) 和用户表多对一 tags(标签) 和标签表多对多
Article_detail文章详情表:
字段: content(文章类容) article(所属文章) 和文章表示一对一 Comment评论表:
字段: content(评论类容) create_time(创建时间) up_count(评论量) user(评论者) 和用户多对一 article(评论文章) 和文章表多对一 parent_comment(父级评论) 和自己自关联(为的是能自己评论自己,可以评论别人的评论)
Comment_poll评论点赞表: 字段: user(点赞的用户) 和用户表多对一 comment(被点赞评论) 和评论表多对一
Article_poll文章点赞表(ArticleUp): 字段: user(点赞的用户) 和用户表多对一 comment(被点赞文章) 和文章表多对一 Tag标签表(Tag):
字段: title(标签名称) bolg(所属博客) 和博客表多对一 Article2Tag文章和标签关系表: 字段: article(文章) 和文章表多对一 tag(标签) 和标签表多对一
三、models中代码实现建表:
1 from django.db import models 2 from django.conf import settings 3 from django.contrib.auth.models import AbstractUser 4 # Create your models here. 5 class UserInfo(AbstractUser): #settings :AUTH_USER_MODEL ="项目名称.UserInfo" 6 '''用户信息表 7 继承了django自带的用户表所以在配置文件中务必要加上AUTH_USER_MODEL='项目名称'.UserInfo 8 ''' 9 10 nid = models.BigAutoField(primary_key=True) 11 nickname =models.CharField(max_length=32,verbose_name="昵称",unique=True) 12 tel = models.IntegerField(verbose_name="电话",unique=True,null=True,blank=True) 13 email = models.CharField(max_length=64,verbose_name="邮箱") 14 avatar = models.FileField(verbose_name="头像",upload_to="avatar",default="/avatar/default.png") 15 create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True) 16 17 class Meta: 18 verbose_name_plural = "用户信息表" 19 def __str__(self): 20 return self.username 21 22 class Article(models.Model): 23 ''' 24 文章表 25 ''' 26 title = models.CharField(max_length=64,verbose_name="文章标题") 27 summary = models.CharField(max_length=244, verbose_name="文章概要") 28 create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True) 29 update_time = models.DateTimeField(verbose_name="修改时间",auto_now=True) 30 poll_count = models.IntegerField(verbose_name="点赞数",default=0) 31 comment_count = models.IntegerField(verbose_name="评论数",default=0) 32 read_count = models.IntegerField(verbose_name="阅读数",default=0) 33 34 user = models.ForeignKey(to="UserInfo",verbose_name="所属作者",null=True,blank=True) 35 classify = models.ForeignKey(to="Classfication",verbose_name="所属类别",null=True,blank=True) 36 tags = models.ManyToManyField(to="Tag",through="Article2tag",through_fields=('article', 'tag'),verbose_name="所属标签") 37 type_choices = [ 38 (1,"编程语言"), 39 (2,"软件设计"), 40 (3,"前端系列"), 41 (4,"数据库"), 42 (5,"操作系统") 43 ] 44 artcle_type_id = models.IntegerField(choices=type_choices,default=None) 45 class Meta: 46 verbose_name_plural = "文章表" 47 def __str__(self): 48 return self.title 49 50 class Article_detail(models.Model): 51 '''文章细节表''' 52 article = models.OneToOneField(to="Article",verbose_name="所属文章") 53 content =models.TextField(max_length=400,verbose_name="文章内容") 54 55 class Meta: 56 verbose_name_plural = "文章细节表" 57 58 class Tag(models.Model): 59 '''标签表''' 60 name = models.CharField(max_length=32,verbose_name="标签名") 61 blog = models.ForeignKey(to="Blog",verbose_name="所属博客") 62 class Meta: 63 verbose_name_plural = "标签表" 64 65 class Article2tag(models.Model): 66 article = models.ForeignKey(verbose_name="文章",to="Article") 67 tag = models.ForeignKey(verbose_name="标签",to="Tag") 68 class Meta: 69 '''联合唯一''' 70 unique_together = [ 71 ("article","tag") 72 ] 73 74 class Comment(models.Model): 75 '''评论表''' 76 time = models.DateTimeField(verbose_name="评论时间",auto_now_add=True) 77 content = models.CharField(max_length=265,verbose_name="评论内容") 78 up_count = models.IntegerField(default=0) 79 user = models.ForeignKey(to="UserInfo",verbose_name="评论人",null=True,blank=True) 80 article = models.ForeignKey(to="Article",verbose_name="评论文章",null=True,blank=True) 81 farther_comment = models.ForeignKey(to="Comment",verbose_name="父级评论",null=True,blank=True) 82 # farther_comment = models.ForeignKey("self",verbose_name="父级评论",null=True,blank=True) 83 84 class Meta: 85 verbose_name_plural = "评论表" 86 87 class Article_poll(models.Model): 88 '''文章点赞表''' 89 time = models.DateTimeField(verbose_name="点赞时间",auto_now_add=True) 90 article = models.ForeignKey(to="Article",verbose_name="点赞文章",null=True,blank=True) #一个文章可以有多个赞 91 user = models.ForeignKey(to="UserInfo",verbose_name="点赞人",null=True,blank=True) 92 is_positive = models.BooleanField(default=1,verbose_name="点赞或踩") 93 94 class Meta: 95 '''联合唯一''' 96 unique_together = ("user", "article",) 97 verbose_name_plural = "文章点赞表" 98 99 class Comment_poll(models.Model): 100 '''评论点赞表''' 101 time=models.DateTimeField(verbose_name="点赞时间",auto_now_add=True) 102 # is_positive = models.BooleanField(verbose_name="点赞或踩",default=1) 103 user = models.ForeignKey(to="UserInfo",verbose_name="点赞用户",null=True,blank=True) 104 comment = models.ForeignKey(to="Comment",verbose_name="点赞所属评论",null=True,blank=True) #一个评论可以有多个赞 105 106 class Meta: 107 '''联合唯一''' 108 unique_together = ("user","comment",) 109 verbose_name_plural = "评论点赞表" 110 111 class Blog(models.Model): 112 '''个人站点表''' 113 title = models.CharField(max_length=32,verbose_name="个人博客标题") 114 url = models.CharField(max_length=64,verbose_name="路径",unique=True) 115 theme = models.CharField(max_length=32,verbose_name="博客主题") 116 user = models.OneToOneField(to="UserInfo", verbose_name="所属用户") 117 class Meta: 118 '''通过admin录入数据的时候个中文显示''' 119 verbose_name_plural = "个人站点表" 120 121 def __str__(self): 122 return self.title 123 124 class Classfication(models.Model): 125 '''博主个人文章分类表''' 126 title = models.CharField(max_length=32, verbose_name="分类标题") 127 blog = models.ForeignKey(to="Blog",verbose_name="所属博客") 128 129 class Meta: 130 verbose_name_plural = "分类表"