for command in dir(hobbys):
try:
print(eval(f'hobbys.{command}()'))
print(eval(f'hobbys.{command}'))
except:
pass
# serializers.py
from rest_framework import serializers
from user.models import User
class HobbySerializer(serializer.ModelSerializer):
same_hobby_users = serializers.SerializerMethodField()
def get_same_hobby_users(self,obj):
user_list = []
# obj hobby model's objects
for user_profile in obj.userprofile_set.all():
user_list.append(user_profile.user.username)
return user_list
class Meta:
model = HobbyModel
fields = ["name"]
class UserProfileSerializer(serializer.ModelSerializer):
hobby = HobbySerializer(many=True) # if input data is QuerySet, need to add 'many=True'
class Meta:
model = UserProfileModel
fields = [
"introduction", "birthday", "age", "hobby"
]
class UserSerializer(serializer.ModelSerializer):
userprofile = UserProfileSerializer() # object
hobby
class Meta:
model = UserModel
fields = '__all__'
# views.py
def get(self, request):
return Response(UserSerializer(request.user).data)
Serializer로 출력되는 결과를 보니, riot API 데이터 출력방식이 serializer라는걸 알았다. 갈고닦을 필요가 있다!