Discussion:
Django - Serialize aggregate result from returned from the raw sql
arashaga
2018-05-25 00:36:58 UTC
Permalink
I am using Django raw function to run a SQL statement on Postgres to get a
count on the database in the view function but I could not figure out how
to setup the serializer for it. Any help will be appreciated it. Below is
my code for view and the serializer.


View:

@api_view()def count_person_time_series(request,person):
try:

serializer = RSerializer(RModel.
objects.raw( "SELECT date,count(*) from rtable where %s =ANY(person) group by date",[person]), many=True)
except RModel.DoesNotExist:
raise Http404("person does not exist")

return Response(serializer.data)

Serilizer:

class RSerializer(serializers.ModelSerializer):
## I could not figure out this part
person_count = serializers.SerializerMethodField()

class Meta:
model = RModel
fields = '__all__'
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...