Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add total doctors count to hospital doctor list response #2629

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
15 changes: 15 additions & 0 deletions care/facility/api/serializers/hospital_doctor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import serializers
from django.db.models import Sum

from care.facility.api.serializers import TIMESTAMP_FIELDS
from care.facility.models import DOCTOR_TYPES, HospitalDoctors
Expand All @@ -8,11 +9,25 @@
class HospitalDoctorSerializer(serializers.ModelSerializer):
area_text = ChoiceField(choices=DOCTOR_TYPES, read_only=True, source="area")
id = serializers.UUIDField(source="external_id", read_only=True)
total_doctors = serializers.IntegerField(read_only=True)

class Meta:
model = HospitalDoctors
read_only_fields = (
"id",
"area_text",
"total_doctors"
)
exclude = (*TIMESTAMP_FIELDS, "facility", "external_id")

def to_representation(self, instance):
representation = super().to_representation(instance)
try:
representation["total_doctors"] = (
HospitalDoctors.objects.filter(facility=instance.facility)
.aggregate(total_doctors=Sum("count"))["total_doctors"]
or 0
)
except Exception as e:
representation["total_doctors"] = 0
return representation