基本的:
models.py
####################################以下都是抽屉的代码####################################from django.db import models# 记录用户注册的次数class SendMsg(models.Model): nid = models.AutoField(primary_key=True) # 自定义id名为nid,默认id名是id【固定格式】 email = models.CharField(max_length=32, unique=True) code = models.CharField(max_length=6) ctime = models.DateTimeField() times = models.IntegerField(default=0)# 新闻类型class NewsType(models.Model): caption = models.CharField(max_length=16)# 抽屉的用户表设计class UserInfo(models.Model): nid = models.AutoField(primary_key=True) username = models.CharField(max_length=32, unique=True) password = models.CharField(max_length=32) email = models.CharField(max_length=32, unique=True) ctime = models.DateTimeField()# 新闻表class News(models.Model): # 可以自己创建id,AutoField且primary_key = True【默认id=id】 title = models.CharField(max_length=64) # 标题 summary = models.CharField(max_length=128, null=True) # [内容,可为空简介] url = models.URLField(null=True) ctime = models.DateTimeField(auto_now_add=True) # 下面2个user效果同,只是写法不同 # user = models.ForeignKey('UserInfo', on_delete=True, to_field='nid') # 跟UserInfo的nid进行关联 user = models.ForeignKey(to="UserInfo", to_field='nid', related_name='n', on_delete=True) # 跟UserInfo表关联 nt = models.ForeignKey(to='NewsType', to_field='id', related_name='tn', on_delete=True) # 关联NewsType表,关联默认的id # 引用下面一行代码,就不需要下面的Favor表了,2个一个效果【这里采用点赞表实现,所以注释了】 # favor = models.ManyToManyField(to='UserInfo') # 默认关联的就是主键 favor_count = models.IntegerField(default=0) # 每点赞一次就增加1,同时给顶踩表添加一条数据 comment_count = models.IntegerField(default=0) # 每评论一次就增加1,同时给评论表添加一条数据# 点赞表class Favor(models.Model): user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True) news = models.ForeignKey(to='News', to_field='id', on_delete=True)# 评论表class Comment(models.Model): news = models.ForeignKey(to='News', to_field='id', on_delete=True) user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True) content = models.CharField(max_length=150) ctime = models.DateTimeField(auto_now_add=True) device = models.CharField(max_length=16, null=True)# 顶踩表:记录你顶过那个用户的哪条新闻,踩过哪个用户的哪条新闻
高级的:
这里使用many2many实现news和Useinfo的多对多的关系
我们在News里面是用来favor_count和comment_count来实现页面显示的点赞个数和评论个数的统计,后续页面有数据过来的时候,我们给favor_count和comment_count加1,且插入数据在comment和favor表中。[虽然消耗了硬盘,但是提高了速度]
models.py
####################################以下都是抽屉的代码####################################from django.db import models# 记录用户注册的次数class SendMsg(models.Model): nid = models.AutoField(primary_key=True) # 自定义id名为nid,默认id名是id【固定格式】 email = models.CharField(max_length=32, unique=True) code = models.CharField(max_length=6) ctime = models.DateTimeField() times = models.IntegerField(default=0)# 新闻类型class NewsType(models.Model): caption = models.CharField(max_length=16)# 抽屉的用户表设计class UserInfo(models.Model): nid = models.AutoField(primary_key=True) username = models.CharField(max_length=32, unique=True) password = models.CharField(max_length=32) email = models.CharField(max_length=32, unique=True) ctime = models.DateTimeField()# 新闻表class News(models.Model): # 可以自己创建id,AutoField且primary_key = True【默认id=id】 title = models.CharField(max_length=64) # 标题 summary = models.CharField(max_length=128, null=True) # [内容,可为空简介] url = models.URLField(null=True) ctime = models.DateTimeField(auto_now_add=True) # 下面2个user效果同,只是写法不同 # user = models.ForeignKey('UserInfo', on_delete=True, to_field='nid') # 跟UserInfo的nid进行关联 user = models.ForeignKey(to="UserInfo", to_field='nid', related_name='n', on_delete=True) # 跟UserInfo表关联 nt = models.ForeignKey(to='NewsType', to_field='id', related_name='tn', on_delete=True) # 关联NewsType表,关联默认的id # 引用下面一行代码,就不需要下面的Favor表了,2个一个效果【不采用点赞表Favor】 favor = models.ManyToManyField(to='UserInfo') # 默认关联的就是主键 favor_count = models.IntegerField(default=0) # 每点赞一次就增加1,同时给顶踩表添加一条数据 comment_count = models.IntegerField(default=0) # 每评论一次就增加1,同时给评论表添加一条数据# 点赞表【这里采用manyTomany实现userInfo和News单独关系,所以注释了】# class Favor(models.Model):# user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True)# news = models.ForeignKey(to='News', to_field='id', on_delete=True)# 评论表class Comment(models.Model): news = models.ForeignKey(to='News', to_field='id', on_delete=True) user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True) content = models.CharField(max_length=150) ctime = models.DateTimeField(auto_now_add=True) device = models.CharField(max_length=16, null=True)# 顶踩表:记录你顶过那个用户的哪条新闻,踩过哪个用户的哪条新闻
终极版:提升速度,增加功能
models.py
####################################以下都是抽屉的代码####################################from django.db import models# 记录用户注册的次数class SendMsg(models.Model): nid = models.AutoField(primary_key=True) # 自定义id名为nid,默认id名是id【固定格式】 email = models.CharField(max_length=32, unique=True) code = models.CharField(max_length=6) ctime = models.DateTimeField() times = models.IntegerField(default=0)# 新闻类型class NewsType(models.Model): caption = models.CharField(max_length=16)# 抽屉的用户表设计class UserInfo(models.Model): nid = models.AutoField(primary_key=True) username = models.CharField(max_length=32, unique=True) password = models.CharField(max_length=32) email = models.CharField(max_length=32, unique=True) ctime = models.DateTimeField()# 新闻表class News(models.Model): # 可以自己创建id,AutoField且primary_key = True【默认id=id】 title = models.CharField(max_length=64) # 标题 summary = models.CharField(max_length=128, null=True) # [内容,可为空简介] url = models.URLField(null=True) ctime = models.DateTimeField(auto_now_add=True) # 下面2个user效果同,只是写法不同 # user = models.ForeignKey('UserInfo', on_delete=True, to_field='nid') # 跟UserInfo的nid进行关联 user = models.ForeignKey(to="UserInfo", to_field='nid', related_name='n', on_delete=True) # 跟UserInfo表关联 # nt = models.ForeignKey(to='NewsType', to_field='id', related_name='tn', on_delete=True) # 关联NewsType表,关联默认的id # 因为内容固定,所以这里直接采用列表的形式了 news_type_choices = [ (1, '42区'), (2, '段子'), (3, '图片'), (4, '挨踢1024'), (5, '你问我答'), ] # DjangoAdmin里面会渲染为一个下拉框,这里需要我们自己去写下拉框了 nt = models.IntegerField(choices=news_type_choices) # 引用下面一行代码,就不需要下面的Favor表了,2个一个效果 favor = models.ManyToManyField(to='UserInfo') # 默认关联的就是主键 favor_count = models.IntegerField(default=0) # 每点赞一次就增加1,同时给顶踩表添加一条数据 comment_count = models.IntegerField(default=0) # 每评论一次就增加1,同时给评论表添加一条数据# 点赞表 【这里我们采用many2many实现多对多的关系,此表暂时不用】# class Favor(models.Model):# user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True)# news = models.ForeignKey(to='News', to_field='id', on_delete=True)# 评论表class Comment(models.Model): news = models.ForeignKey(to='News', to_field='id', on_delete=True) user = models.ForeignKey(to='UserInfo', to_field='nid', on_delete=True) content = models.CharField(max_length=150) ctime = models.DateTimeField(auto_now_add=True) device = models.CharField(max_length=16, null=True) # 这里要求父评论只能是数字且是Comment里面的自增ID # 要求也能实现反向查找【此时可认为是2个Comment表,但需要添加releat_name,否则就易混乱】 parent_comment = models.ForeignKey(to="Comment", null=True, on_delete=True, related_name='pc')# 顶踩表:记录你顶过那个用户的哪条新闻,踩过哪个用户的哪条新闻