JOOHUUN
django | 유저 프로필, 취미 serializers 본문
# 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