{
"id": 2,
"password": "pbkdf2_sha256$600000$f1MvwCTEziLP0LJxoOxcWe$MexstDPotQ5YQ25w1JH5KK9hHgKrMyCDLKIwQqnws3s=",
"last_login": null,
"email": "[email protected]",
"username": "testuser2",
"is_expert": false,
"is_active": true,
"is_admin": false,
"following": []
}
{
"id": 2,
"email": "[email protected]",
"username": "testuser2",
"following": [],
"followers": [
"testuser3"
]
}
class UserProfileSerializer(serializers.ModelSerializer):
**followers = serializers.StringRelatedField(many=True)**
class Meta:
model = User
fields = ('id', 'email', 'username', 'following', 'followers')
작성했던 글까지 확인가능하도록 변경하기
{
"id": 2,
"email": "[email protected]",
"username": "testuser2",
"following": [],
"followers": [
"testuser3"
],
"article_set": [
{
"article_id": 5,
"content": "수정된 게시물 1",
"image": null,
"created_at": "2024-08-15T18:53:01.849487+09:00",
"user": "testuser2",
"likes_count": 0,
"comment_count": 0
},
{
"article_id": 6,
"content": "This is content of article2",
"image": null,
"created_at": "2024-08-15T18:55:35.261683+09:00",
"user": "testuser2",
"likes_count": 0,
"comment_count": 0
}
]
}
내가 좋아요한 글들을 가져오기
likes = models.ManyToManyField(User, related_name='like_articles')
class UserProfileSerializer(serializers.ModelSerializer):
#팔로워
followers = serializers.StringRelatedField(many=True)
#사용자가 작성한 글들 확인
article_set = ArticleListSerializer(many=True)
#사용자가 좋아요한 글들 확인
like_articles = ArticleListSerializer(many=True)
class Meta:
model = User
fields = ('id', 'email', 'username', 'following', 'followers', 'article_set', 'like_articles')
{
"id": 4,
"email": "[email protected]",
"username": "testuser3",
"following": [
2
],
"followers": [],
"article_set": [
{
"article_id": 8,
"content": "토큰 발급 성공 후 수정한 게시물",
"image": null,
"created_at": "2024-08-15T19:24:34.219196+09:00",
"user": "testuser3",
"likes_count": 1,
"comment_count": 2
},
{
"article_id": 9,
"content": "This is content of article4",
"image": null,
"created_at": "2024-08-15T23:11:24.056574+09:00",
"user": "testuser3",
"likes_count": 0,
"comment_count": 0
},
{
"article_id": 10,
"content": "This is content of article5",
"image": null,
"created_at": "2024-08-15T23:18:53.826000+09:00",
"user": "testuser3",
"likes_count": 0,
"comment_count": 0
}
],
"like_articles": [
{
"article_id": 8,
"content": "토큰 발급 성공 후 수정한 게시물",
"image": null,
"created_at": "2024-08-15T19:24:34.219196+09:00",
"user": "testuser3",
"likes_count": 1,
"comment_count": 2
},
{
"article_id": 5,
"content": "수정된 게시물 1",
"image": null,
"created_at": "2024-08-15T18:53:01.849487+09:00",
"user": "testuser2",
"likes_count": 1,
"comment_count": 0
}
]
}