0

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')
3
  • I need to find the length of the array of each encountersDaily and find the sum of it. Commented Aug 1, 2022 at 1:46
  • Can you add the content of the models.py, and views.py file? Commented Aug 1, 2022 at 2:39
  • I added the models, views and serializers used. Commented Aug 1, 2022 at 4:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its working now. I removed the Subquery in the code that you've sent and it works perfectly. thanks. I'll just read the documentation about annotate to have more knowledge about it. count = Promoter.objects.filter( promotersGroup__GroupID=request.data.get('groupId') ).values( "id", "user_id", "encountersDaily" ).annotate(number_of_encounter=Count('encountersDaily')) this is the working code that I used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.