一、模型类
class ArticleManager(models.Manager): def archive(self, **kwargs): l = [] month_list = [] for i in self.filter(**kwargs).values("create_time"): date = i[‘create_time‘] month_list.append(date.month) t = date.strftime("%Y-%m-%d") if t not in l: l.append(t) blog_age = str(len(set(month_list))) + "个月" return l,blog_age class UserInfo(AbstractUser): """ 用户信息 """ nid = models.BigAutoField(primary_key=True) nickname = models.CharField(verbose_name=‘昵称‘, max_length=32) telephone = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name=‘手机号码‘) avatar = models.FileField(verbose_name=‘头像‘, upload_to=‘./upload/avatar/‘) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) fans = models.ManyToManyField(verbose_name=‘粉丝们‘, to=‘UserInfo‘, through=‘UserFans‘, through_fields=(‘user‘, ‘follower‘)) def __str__(self): return self.username class UserFans(models.Model): """ 互粉关系表 """ nid = models.AutoField(primary_key=True) 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‘), ] 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‘) def __str__(self): return self.title 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‘) def __str__(self): return self.title class Article(models.Model): nid = models.BigAutoField(primary_key=True) title = models.CharField(max_length=50, verbose_name=‘文章标题‘) desc = models.CharField(max_length=255, verbose_name=‘文章描述‘) read_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) up_count = models.IntegerField(default=0) down_count = models.IntegerField(default=0) category = models.ForeignKey(verbose_name=‘文章类型‘, to=‘Category‘, to_field=‘nid‘, null=True) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘) tags = models.ManyToManyField( to="Tag", through=‘Article2Tag‘, through_fields=(‘article‘, ‘tag‘), ) type_choices = [ (1, "Python"), (2, "Linux"), (3, "OpenStack"), (4, "GoLang"), ] article_type_id = models.IntegerField(choices=type_choices, default=None) objects = ArticleManager() def __str__(self): return self.title # class ArticleDetail(models.Model): """ 文章详细表 """ nid = models.AutoField(primary_key=True) content = models.TextField(verbose_name=‘文章内容‘, ) article = models.OneToOneField(verbose_name=‘所属文章‘, to=‘Article‘, to_field=‘nid‘) def __str__(self): return self.article.title class Comment(models.Model): """ 评论表 """ nid = models.BigAutoField(primary_key=True) article = models.ForeignKey(verbose_name=‘评论文章‘, to=‘Article‘, to_field=‘nid‘) content = models.CharField(verbose_name=‘评论内容‘, max_length=255) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) parent_id = models.ForeignKey(‘self‘, blank=True, null=True, verbose_name=‘父级评论‘) user = models.ForeignKey(verbose_name=‘评论者‘, to=‘UserInfo‘, to_field=‘nid‘) up_count = models.IntegerField(default=0) class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey(‘UserInfo‘, null=True) article = models.ForeignKey("Article", null=True) UporDown = models.BooleanField(verbose_name=‘是否赞‘,default=False) class CommentUp(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey(‘UserInfo‘, null=True) comment = models.ForeignKey("Comment", null=True) 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‘) def __str__(self): return self.title class Article2Tag(models.Model): nid = models.AutoField(primary_key=True) 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‘), ]
二、数据查询
前提,前端传来文章的作者名。
def blog(request, name): userinfo = UserInfo.objects.filter(username=name).first() if not userinfo: return render(request,‘404.html‘) currentblog = Blog.objects.filter(user=userinfo).first() # 过滤出当前作者的博客 正向查询 artcle_list = Article.objects.filter(blog=currentblog) # 查询出该博客所有文章 正向查询 catagory_list = Category.objects.filter(blog=currentblog) # 查询出该博客所有分类 正常查询 一对多关系 # 查询所有分类对应文章的数量 # 方式一 ret = [] for catagory in catagory_list: tmp = [] tmp.append(catagory) tmp.append(catagory.article_set.all().count()) # 方式二 catagory_count = artcle_list.values_list("category__title").annotate(Count("nid")) # 统计每个分类的文章数目,反向查询 # 标签对应文章的数目 tag_list = Tag.objects.filter(blog=currentblog) # 方式一 res = [] for tag in tag_list: temp = [] tmp.append(tag.title) tmp.append(artcle_list.filter(tags__title=tag.title).count()) res.append(tmp) # 方式二 tag_cout = artcle_list.values_list("tags__title").annotate(Count(‘nid‘)) # 文章按时间分类 ress1,blog_age = Article.objects.archive(blog=currentblog) return render(request, ‘homesite.html‘, {"userinfo": userinfo,‘catagory_count‘:catagory_count, ‘tag_cout‘:tag_cout,‘ress1‘:ress1,‘blog_age‘:blog_age, })
三、前端显示
后台查询到的数据,通过模板语言渲染后,显示在前端!
时间: 2024-10-10 21:07:47