rest api django return value if exists in another table

Solutions on MaxInterview for rest api django return value if exists in another table by the best coders in the world

showing results for - "rest api django return value if exists in another table"
Caden
03 Feb 2019
1class PostSerializer(serializers.ModelSerializer):
2    replied = serializers.SerializerMethodField('has_replies')
3
4    def has_replies(post):
5        return post.replies.filter(owner=self.context["request"].user).exists()
6
7    class Meta:
8        fields = ('id', 'name', 'replied')
Manuela
20 Feb 2018
1class PostList(generics.ListAPIView):
2    ...
3    def get_queryset(self):
4        Post.objects.all().extra(select={
5        'current_user_replies_count': 'SELECT COUNT(*) FROM <reply table> WHERE' +
6        'post_id=posts_post.id AND owner_id = %s'
7                                  },select_params=(request.user.id,))
similar questions