How can you include annotated results in a serialized QuerySet?
data = serializer.serialize(Books.objects.filter(publisher__id=id).annotate(num_books=Count(
As shown in this post you can use SerializerMethodField in your Serializer:
class BooksSerializer(serializers.ModelSerializer):
num_books = serializers.SerializerMethodField()
def get_num_books(self, obj):
try:
return obj.num_books
except:
return None
It will serialize the annotated value (readonly)