Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have the following models, models.py

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

mathces_won=Count('match')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading