0

I got a problem with annotate method when I was using the Count method to count multiple columns that come from the database which have a relationship with one of the tables.

Let me give you a quick example:

match_session_instance = MatchSessionInstance.objects.filter(match_session=match_session, status="main")

match_instances = MatchSessionInstance.objects.filter(match_session=match_session)

action_counts = match_instances.values(player_number=F("player_pk__number"), player_name=F("player_pk__player"))\
                    .annotate(pass_count=Count("live_match_pass__id", distinct=True),
                              corner_count=Count("live_match_corner__id", distinct=True))

In the meantime, I'm not facing any problems - I caught my issue and I addressed it but that is the problem now.

I don't know how could "disticnt=True" parameter helps me to fix that problem!

I googled a bit and found this source that has helped me: Count on multiple fields in Django querysets

I know what does distinct as a method in ORM but actually, I get no idea how it works in that format special when I used columns that never have duplicated data.

How can I understand this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdelhamed Abdin
  • 556
  • 1
  • 6
  • 19

0 Answers0