<Django> [instamg] feeddetailpage

jm_yoon·2021년 2월 24일
0

기업협업

목록 보기
3/6
post-custom-banner


인스타 피드에서 게시물을 누를 때 나오는 페이지

  • 필요한 데이터
    post, user_pofile, user_account, post_content, 로그인한 유저가 계정주와 팔로우 한 사이인지TF, 댓글, 대댓글....
'comments'       : [{
                        'comment_id'    : comment.id,
                        'user_id'       : comment.user_id_id,
                        'account'       : comment.user_id.account,
                        'profile_photo' : comment.user_id.profile_photo,#"/media/"+ str(comment.user_id.thumbnail_path) if str(comment.user_id.thumbnail_path) else None,
                        'content'       : comment.content,
                        'created_at'    : comment.created_at,
                        'like_count'    : Like.objects.filter(comment_id_id=comment.id).count()
                    }for comment in post.comments.filter(comment_id_id=None)],
'recomments'     : [{
                        'recomment_id'  : comment.id,
                        'comment_id'    : comment.comment_id_id,
                        'user_id'       : comment.user_id_id,
                        'account'       : comment.user_id.account,
                        'profile_photo' : comment.user_id.profile_photo,#"/media/"+ str(comment.user_id.thumbnail_path) if str(comment.user_id.thumbnail_path) else None,
                        'content'       : comment.content,
                        'created_at'    : comment.created_at,
                        'like_count'    : Like.objects.filter(comment_id_id=comment.id).count(),
                    }for comment in post.comments.exclude(comment_id_id=None)],
 'file'          :[{
                                    'file_type'      : post_attach_file.file_type,
                                    "view_count"     : post_attach_file.view_count,
                                    'path'           : "/media/"+str(post_attach_file.path),
                                    'thumbnail_path' : "/media/"+str(post_attach_file.thumbnail_path)
                    }for post_attach_file in post.post_attach_files.all()]
            }
            return JsonResponse({'post':post}, status=200)
        except ValueError:
            return JsonResponse({'message':'VALUE_ERROR'}, status=400)
        except IndexError:
            return JsonResponse({'message':'POST_DOES_NOT_EXIST'}, status=400)

처음에 썼던 방법
비효율 적인 느낌👎

'comments'           : [{
                                        'comment_id'                 : comment.id,
                                        'content'                    : comment.content,
                                        'comment_user_id'            : comment.user_id.id,
                                        'comment_user_account'       : comment.user_id.account,
                                        'comment_user_profile_photo' : "/media/"+ str(comment.user_id.thumbnail_path) if str(comment.user_id.thumbnail_path) else None,
                                        'created_at'                 : comment.created_at,
                                        'like_count'                 : Like.objects.filter(id=comment.id).count(),
                                        'is_liked'                   : comment.likes.exists(),
      'recomment'                  :[{
                                            'comment_id'                   : comment.id,
                                            'recomment_id'                 : recomment.id,
                                            'content'                      : recomment.content,
                                            'recomment_user_id'            : recomment.user_id.id,
                                            'recomment_user_account'       : recomment.user_id.account,
                                            'recomment_user_profile_photo' : "/media/"+ str(recomment.user_id.thumbnail_path) if str(recomment.user_id.thumbnail_path) else None,
                                            'created_at'                   : recomment.created_at,
                                            'like_count'                   : Like.objects.filter(comment_id_id=comment.id).count(),
                                            'is_liked'                     : comment.likes.exists()
                } for recomment in Comment.objects.filter(comment_id=comment.id)]
            } for comment in post.comments.filter(comment_id=None)],

수정한 코드
comments 라는 배열안에 키값이 recomments인 배열을 가지고있음

profile
Hello!
post-custom-banner

0개의 댓글