class FollowingSerializer(serializers.ModelSerializer): user = UserSerializerForFriendship(source='to_user') # 此处created_at也可以不写 系统默认的就是DateTimeField created_at = serializers.DateTimeField() class Meta: model = Friendship fields = ('user', 'created_at')
class FollowerSerializer(serializers.ModelSerializer): user = UserSerializerForFriendship(source='from_user') # 程序怎么知道要取哪个model? 是通过Meta里的model? # 此处created_at也可以不写 系统默认的就是DateTimeField created_at = serializers.DateTimeField() class Meta: model = Friendship fields = ('user', 'created_at')
class FollowerSerializer(serializers.ModelSerializer, FollowingUserIdSetMixin): user = UserSerializerForFriendship(source='cached_from_user') has_followed = serializers.SerializerMethodField() class Meta: model = Friendship fields = ('user', 'created_at', 'has_followed') def get_has_followed(self, obj): return obj.from_user_id in self.following_user_id_set
class FollowingSerializer(serializers.ModelSerializer): user = UserSerializerForFriendship(source='to_user') created_at = serializers.DateTimeField() has_followed = serializers.SerializerMethodField() class Meta: model = Friendship fields = ('user', 'created_at', 'has_followed') def get_has_followed(self, obj): if self.context['request'].user.is_anonymous: return False return FriendshipService.has_followed(self.context['request'].user, obj.to_user)
class FollowerSerializer(serializers.ModelSerializer, FollowingUserIdSetMixin): user = UserSerializerForFriendship(source='cached_from_user') created_at = serializers.DateTimeField() has_followed = serializers.SerializerMethodField() def get_has_followed(self, obj): if self.context['request'].user.is_anonymous: return False return obj.from_user_id in self.following_user_id_set class Meta: model = Friendship fields = ( 'user', 'created_at', 'has_followed', )
def get_user(self, obj): user = UserService.get_user_by_id(self.get_user_id(obj)) return UserSerializerForFriendship(user).data
class FollowingSerializer(serializers.ModelSerializer): user = UserSerializerForFriendship(source='to_user') class Meta: model = Friendship fields = ('user', 'created_at')