imkost
imkost

Reputation: 8163

django models filter ordering

I do such a thing:

Player.objects.distinct().filter(Q(game1_set=game) | Q(game2_set=game))

It returns all players, that played game. I want the result to be orderd, like in filter: game1_set first and then game2_set. But it's not so. It's orderd by id.

Also, I need a QuerySet result, so, merging two lists is not an option.

Here is a models.py for better understanding:

class Player(models.Model):
    game1_set = models.ManyToManyField('Game', verbose_name='players1')
    game2_set = models.ManyToManyField('Game', verbose_name='players2')

class Game(models.Model):
    # some fields here

Upvotes: 0

Views: 133

Answers (1)

FGiL
FGiL

Reputation: 36

try adding .order_by('game1_set','game2_set')

Player.objects.distinct().filter(Q(game1_set=game) | Q(game2_set=game)).order_by('game1_set','game2_set')

Upvotes: 2

Related Questions