Cannot resolve keyword into field. Choices are following, in Django

I have the following models,

class Account(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=255, null=True)
    profile_pic = models.ImageField(null=True, blank=True)
    ratings = models.FloatField(default=1000)
    date_joined = models.DateTimeField(auto_now_add=True, null=True)
    phone = models.CharField(max_length=255, null=True)

class Match(models.Model):

    match_time = models.DateTimeField(null=True)
    totalPlayers = models.IntegerField(default=2)
    winner = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True)

    class Meta:
        ordering = ['-match_time']

class Participant(models.Model):
    match = models.ForeignKey(Match, on_delete=models.CASCADE)
    player = models.ForeignKey(Account, on_delete=models.CASCADE)
    player_points = models.FloatField(null=True)

What I want is basically to write a Query that can return me the following things, Player_Name, Total Matches by Player, Total Points of Player, Matches Won by the Player, Player Ratings

I wrote a query like this and it worked great for all the columns above, except Total Points of Player and Matches Won by the Player

players = Account.objects.annotate(matches=Count('participant')).order_by('-ratings')

So, following the same principle, I tried the following Query to get what I needed exactly,

players = Account.objects.annotate(matches=Count('participant'), total_points=Count('player_points'), matches_won=Count('winner')).order_by('-ratings')

But this is giving me the following error,

Cannot resolve keyword 'player_points' into field. Choices are: date_joined, id, match, matches, name, participant, phone, profile_pic, ratings, user, user_id

I unfortunately do not understand how I can get my required result. Can anyone help me reach what I am trying to do?

>Solution :

The issue is that player_points are not in the account model, it is in the participant.

players = Account.objects.annotate(matches=Count('participant'), total_points=Count('player_points'), matches_won=Count('winner')).order_by('-ratings')

For the Total Points of Player use this in the annotate.

total_points=Sum('participant__player_points') # By the description i think you are looking the Sum of the point as a total not the Count, but you can change it if not.

For the Matches Won by the Player:


Leave a Reply