Answers for "rest api django return value if exists in another table"

0

rest api django return value if exists in another table

class PostSerializer(serializers.ModelSerializer):
    replied = serializers.SerializerMethodField('has_replies')

    def has_replies(post):
        return post.replies.filter(owner=self.context["request"].user).exists()

    class Meta:
        fields = ('id', 'name', 'replied')
Posted by: Guest on May-08-2020
-1

rest api django return value if exists in another table

class PostList(generics.ListAPIView):
    ...
    def get_queryset(self):
        Post.objects.all().extra(select={
        'current_user_replies_count': 'SELECT COUNT(*) FROM <reply table> WHERE' +
        'post_id=posts_post.id AND owner_id = %s'
                                  },select_params=(request.user.id,))
Posted by: Guest on May-08-2020

Code answers related to "TypeScript"

Browse Popular Code Answers by Language