파이썬 장고 실무 심화
StringRelatedField
class UserProfileSerializer(serializers.ModelSerializer):
followers = serializers.StringRelatedField(many=True) # email로 표현
followings = serializers.StringRelatedField(many=True)
class Meta:
model = User
fields = ("id","email", "followings", "followers")
{ "id": 1, "email": "admin@aaa.aaa", "followings": [ "test1@aaa.aaa", "test2@aaa.aaa" ], "followers": [ "test1@aaa.aaa" ] }
PrimaryKeyRelatedField
class UserProfileSerializer(serializers.ModelSerializer):
followers = serializers.PrimaryKeyRelatedField(many=True, read_only=True) # id 값으로 표현
followings = serializers.StringRelatedField(many=True)
class Meta:
model = User
fields = ("id","email", "followings", "followers")
{ "id": 1, "email": "admin@aaa.aaa", "followings": [ "test1@aaa.aaa", "test2@aaa.aaa" ], "followers": [ 2 ] }