Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare value of 2 fields in Django QuerySet?

I have a django model like this:

class Player(models.Model):
    name = models.CharField()
    batting = models.IntegerField()
    bowling = models.IntegerField()

What would be the Django QuerySet equivalent of the following SQL?

SELECT * FROM player WHERE batting > bowling;
like image 710
Imran Avatar asked Apr 27 '09 21:04

Imran


1 Answers

In django 1.1 you can do the following:

players = Player.objects.filter(batting__gt=F('bowling'))

See the other question for details

like image 192
Sergey Golovchenko Avatar answered Oct 05 '22 22:10

Sergey Golovchenko