Created
August 30, 2020 11:53
-
-
Save mentix02/022f7b5d258dfdb93aab202e285b53d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSerializer(serializers.ModelSerializer): | |
user = UserListSerializer() | |
images = ImageListSerializer(many=True) | |
is_liked = serializers.SerializerMethodField(allow_null=True) | |
class Meta: | |
model = Post | |
exclude = ['timestamp', 'vote_score', 'num_vote_down'] | |
def get_is_liked(self, post): | |
user = None | |
request = self.context.get('request') | |
if request and hasattr(request, 'user'): | |
user = request.user | |
if user: | |
return post.votes.exists(user.id) | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment