# models.py
# 프로필
class Profile(models.Model):
profile_image = models.ImageField('프로필 사진', default='default_profile_pic.jpg', upload_to='profile_pics', validators=[validate_image_file_extension])
nickname = models.CharField('닉네임', max_length=10, null=True, unique=True, error_messages={"unique": "이미 사용중인 닉네임 이거나 탈퇴한 닉네임입니다."})
intro = models.CharField('자기소개', max_length=100, null=True)
review_cnt = models.PositiveIntegerField('리뷰수', default=0)
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="회원", related_name='user_profile')
followings = models.ManyToManyField('self', symmetrical=False, blank=True, related_name= 'followers')
# serializers.py
# 공개 프로필 serializer
class PublicProfileSerializer(serializers.ModelSerializer):
followings = PrivateProfileSerializer(many=True)
followers = PrivateProfileSerializer(many=True)
**review_set = ReviewListSerializer(many=True, source='user.review_set')**
**bookmark_place = PlaceSerializer(many=True, source='user.bookmark_place')**
user_id = serializers.SerializerMethodField()
def get_user_id(self, obj):
return obj.user.id
class Meta:
model = Profile
fields = ('id', 'user_id', 'nickname', 'profile_image', 'intro', 'followings', 'followers', 'review_set', 'bookmark_place',)
- bookmark_place = PlaceSerializer(many=True, source='user.bookmark_place')
- profile과 OneToOne으로 연결되어있는 user의 북마크, 리뷰에 접근하기 위해 source를 사용