【djangoframework序列化】序列化添加自定义内容 例子:二级回复评论

## 表的设计 一定要空出N个属性出来给自定义添加内容
# models.py

# 图库评论
class ImageComment(models.Model):

  user = models.ForeignKey(to="UserInfo", verbose_name="评论用户")
  image = models.ForeignKey(to="Images", verbose_name="评论图库")
  commenty_txt = models.CharField(verbose_name="评论内容", max_length=255)
  children = models.CharField(verbose_name="子评论", max_length=255,null=True,blank=True)
  time = models.DateTimeField(auto_now=True, verbose_name="评论时间")
  zan = models.IntegerField(verbose_name="点赞次数", default=0)


  class Meta:
    db_table = 'ImageComment'
    verbose_name_plural="图库评论"

 

# 六合图库二级评论回复
class ImageSecondComment(models.Model):

  user_id = models.IntegerField(verbose_name="回复用户ID")
  father_image_comment = models.ForeignKey(to="ImageComment", verbose_name="回复父评论主键",related_name="father_image_comments")
  text = models.CharField(verbose_name="回复内容", max_length=255)
  reply_user_id = models.IntegerField(verbose_name="被回复对象ID")
  reply_usrname = models.CharField(verbose_name="被回复对象用户名",max_length=255)
  time = models.DateTimeField(auto_now=True, verbose_name="回复时间")


  class Meta:
    db_table = 'ImageSecondComment'
    verbose_name_plural="图库二级评论回复"

 

## 创建测试数据


 


 


 

 

 

 

## 自定义添加内容 主要代码部分


# 取出主评论数据
s_data = models.ImageComment.objects.filter(image_id=image_pk).order_by('-id')

## 添加开始  
for image_comment_obj in s_data:
  # 属于这个当前主键下的子评论 都储存在这个数组里面 赋值给添加的属性
  children = []
  for x in models.ImageSecondComment.objects.filter(father_image_comment_id=image_comment_obj.pk):
    children_dict = {}
    children_dict["user_id"] = x.user_id

    children_dict["user_head"] = models.UserInfo.objects.get(pk=x.user_id).head
    children_dict["text"] = x.text
    children_dict["reply_user_id"] = x.reply_user_id
    children_dict["reply_usrname"] = x.reply_usrname
    children_dict["time"] = x.time.strftime('%Y-%m-%d %H:%M:%S')

    children.append(children_dict)


  image_comment_obj.children = children  # 添加完内容后,给创建的空字段赋值(就是自定的内容)


## 添加结束  



 

## 结果:
{
    "code": 200,
    "data": [
        {
            "id": 5,
            "time": "2020-12-09 14:42:52",
            "commenty_txt": "123",
            "children": "[]",
            "zan": 0,
            "user": 2,
            "image": 1
        },
        {
            "id": 4,
            "time": "2020-12-09 14:42:52",
            "commenty_txt": "123",
            "children": "[{'user_id': 5, 'user_head': '/static/default/head.png', 'text': '没错', 'reply_user_id': 1, 'reply_usrname': '15wehjd', 'time': '2020-12-09 16:06:27'}]",
            "zan": 0,
            "user": 1,
            "image": 1
        },
        {
            "id": 2,
            "time": "2020-12-09 14:42:35",
            "commenty_txt": "22",
            "children": "[]",
            "zan": 0,
            "user": 2,
            "image": 1
        },
        {
            "id": 1,
            "time": "2020-12-09 14:42:35",
            "commenty_txt": "123",
            "children": "[{'user_id': 2, 'user_head': '/static/default/head.png', 'text': '嗯嗯', 'reply_user_id': 1, 'reply_usrname': '15wehjd', 'time': '2020-12-09 16:05:27'}, {'user_id': 1, 'user_head': '/static/default/head.png', 'text': '没错', 'reply_user_id': 1, 'reply_usrname': '15wehjd', 'time': '2020-12-09 16:06:27'}]",
            "zan": 0,
            "user": 1,
            "image": 1
        }
    ]
}

 

posted @ 2020-12-10 14:19  PythonNew_Mr.Wang  Views(194)  Comments(0Edit  收藏  举报