Python学习---抽屉框架分析[数据库设计分析]180313
基本的:
models.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | ####################################以下都是抽屉的代码#################################### 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ####################################以下都是抽屉的代码#################################### 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | ####################################以下都是抽屉的代码#################################### 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' ) # 顶踩表:记录你顶过那个用户的哪条新闻,踩过哪个用户的哪条新闻 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 上周热点回顾(1.20-1.26)