Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags more
Archives
Today
Total
관리 메뉴

JOOHUUN

django | 유저 프로필, 취미 serializers 본문

Django

django | 유저 프로필, 취미 serializers

JOOHUUN 2022. 6. 19. 20:52
# user/serializers.py
class HobbySerializer(serializers.ModelSerializer): 
    same_hobby_users = serializers.SerializerMethodField()
    def get_same_hobby_users(self, obj):
    print(obj)
    user_list = []
        for i in obj.hobby.all():
            user_list.append(i.user.username)
        return user_list
    
    class Meta:
        model = Hobby
        fields = ["name"]
        

class ProfileSerializer(serializers.ModelSerializer):
    hobby = HobbySerializer(many=True)   # 유저프로필과 취미는 매니투매니 관계
    class Meta:
        model = Profile
        fields = ["introduction", "birthday", "age", "hobby"]


class UserSerializer(serializers.ModelSerializer):
    profile = ProfileSerializer()   # 유저프로필과 유저는 원투원 관계
    class Meta:
        model = User
        fields = ["username", "password", "fullname", "email", "profile"]
        # fields = "__all__"

 

print(obj), Hobby모델의 오브젝트

 

'Django' 카테고리의 다른 글

django websocket DM  (0) 2022.07.09
django ProductView get, post, put  (0) 2022.06.21
django ViewSet 사용 댓글 작성 및 불러오기  (0) 2022.06.20
django 쿼리문법  (0) 2022.06.20
django | 정참조, 역참조 유저 profile, hobby  (0) 2022.06.18
Comments