Невозможно преобразовать ключевое слово в поле. В Django возможны следующие варианты
У меня есть следующие модели, 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)
В основном я хочу написать запрос, который может вернуть мне следующие вещи, Player_Name
, Total Matches by Player
, Total Points of Player
, Matches Won by the Player
, Player Ratings
Я написал такой запрос, и он отлично сработал для всех столбцов выше, кроме Total Points of Player
и Matches Won by the Player
players = Account.objects.annotate(matches=Count('participant')).order_by('-ratings')
Итак, следуя тому же принципу, я попробовал следующий запрос, чтобы получить то, что мне нужно,
players = Account.objects.annotate(matches=Count('participant'), total_points=Count('player_points'), matches_won=Count('winner')).order_by('-ratings')
Но это дает мне следующую ошибку,
Cannot resolve keyword 'player_points' into field. Choices are: date_joined, id, match, matches, name, participant, phone, profile_pic, ratings, user, user_id
К сожалению, я не понимаю, как я могу получить требуемый результат. Может ли кто-нибудь помочь мне достичь того, что я пытаюсь сделать?
Проблема в том, что player_points
находятся не в модели счета, а в модели участника.
players = Account.objects.annotate(matches=Count('participant'), total_points=Count('player_points'), matches_won=Count('winner')).order_by('-ratings')
Для Total Points of Player
используйте это в аннотации.
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.
Для Matches Won by the Player
:
mathces_won=Count('match')