Home > Software design >  How to count the number of a nested array in django?
How to count the number of a nested array in django?

Time:08-02

I am new to django and I'm still figuring out how use all the functions. This is the result of the query that I want to count

{
            "id": 1,
            "user_id": 1,
            "encountersDaily": [
                {
                    "id": 1,
                    "DateTime": "2022-08-01T01:22:00Z",
                    "Longtitude": "14.536480570700000",
                    "Latitude": "121.049722723900000",
                    "EncounterExitID": 1,
                    "PromoterID": 1,
                    "GenderID": 1
                },
                {
                    "id": 10,
                    "DateTime": "2022-08-01T01:42:46Z",
                    "Longtitude": "14.536480570700000",
                    "Latitude": "121.049722723900000",
                    "EncounterExitID": 1,
                    "PromoterID": 1,
                    "GenderID": 1
                }
            ],
            
        },
        {
            "id": 4,
            "user_id": 4,
            "encountersDaily": [
                {
                    "id": 6,
                    "DateTime": "2022-08-01T01:42:09Z",
                    "Longtitude": "14.536480570700000",
                    "Latitude": "121.049722723900000",
                    "EncounterExitID": 2,
                    "PromoterID": 4,
                    "GenderID": 1
                },
                {
                    "id": 8,
                    "DateTime": "2022-08-01T01:42:29Z",
                    "Longtitude": "14.536480570700000",
                    "Latitude": "121.049722723900000",
                    "EncounterExitID": 1,
                    "PromoterID": 4,
                    "GenderID": 1
                },
                {
                    "id": 9,
                    "DateTime": "2022-08-01T01:42:38Z",
                    "Longtitude": "14.536480570700000",
                    "Latitude": "121.049722723900000",
                    "EncounterExitID": 1,
                    "PromoterID": 4,
                    "GenderID": 2
                }
            ],
            
        }

I have this data of 2 users and my goal is to loop through this array and find the sum of the encountersDaily. is it possible to just use filter function here? or loop is necessary .

views.py

class TotalEncounterPerGroupView(APIView):
    authentication_classes = [JWTAuthentication]

    def get(self, request):
        data = Promoter.objects.all().filter(
            promotersGroup__GroupID=request.data.get('groupId'))

        data_serializer = PromoterNestedSerializer(data, many=True)
        return Response({"user": str(request.user.id), "data": data_serializer.data}, status=status.HTTP_200_OK)

model.py

class PromotersGroup(models.Model):
    GroupID = models.ForeignKey(Group, on_delete=models.CASCADE)
    PromoterID = models.ForeignKey(
        Promoter, related_name='promotersGroup',  on_delete=models.CASCADE, blank=True)

serializer.py

class PromoterNestedSerializer(serializers.ModelSerializer):
    encountersDaily = EncounterDailySerializer(read_only=True, many=True)
    promotersGroup = PromotersGroupSerializer(read_only=True, many=True)

    class Meta:
        model = Promoter
        fields = ('id', 'user_id', 'encountersDaily', 'promotersGroup')

CodePudding user response:

From what ever I can understand from your code given. Your query should be something like this.

 from django.db.models import Count

 CountPromoter.objects.filter(
      promotersGroup__GroupID=request.data.get('groupId')
 ).values(
    "id", "user_id", "encountersDaily"
 ).annotate(
     encountersDaily_count=Subquery(Count('encountersDaily')
 )

You can have count inside a subquery. Need more information about the other two models for me to write more accurate query. Yes and if you have less number of fields in query response then try to fetch them in values or in value_set rather than fetching entire response.

  • Related