Q를 이용해 쿼리문 쓰는 방법
# articles/views.py
class FeedView(APIView): # 로그인된 사람만 쓸 수 있기 때문에 permissions
permission_classes = [permissions.IsAuthenticated]
def get(self, request):
q = Q()
for user in request.user.followings.all(): # 내가 팔로우하는 모든 유저의 쿼리셋
q.add(Q(user=user),q.OR)
feeds = Article.objects.filter(q) # follow하는 사람의 글을 모두 가져오기
serializer = ArticleListSerializer(feeds, many=True) # 시리얼라이저 가져오기
return Response(serializer.data)
# articles/urls.py
from django.urls import path
from articles import views
urlpatterns = [
path('', views.ArticleView.as_view(), name='article_view'),
path('feed/', views.FeedView.as_view(), name='feed_view'), # 추가
path('<int:article_id>/', views.ArticleDetailView.as_view(), name='article_detail_view'),
path('<int:article_id>/comment/', views.CommentView.as_view(), name='comment_view'),
path('<int:article_id>/comment/<int:comment_id>', views.CommentDetailView.as_view(), name='comment_detail_view'),
path('<int:article_id>/like/', views.LikeView.as_view(), name='like_view'),
]