This tutorial is about how to implement SQL-like group by queries using the Django ORM. It’s a fairly common operation, specially for those who are familiar with SQL. The Django ORM is actually an abstraction layer, that let us play with the database as it was object-oriented but in the end it’s just a relational database and all the operations are translated into SQL statements.
Most of the work can be done retrieving the raw data from the database, and playing with it in the python side, grouping the data in dictionaries, iterating through it, making sums, averages and what not. But the database is a very powerful tool and do much more than simply storing the data, and often you can do the work much faster directly in the database.
Generally speaking, when you start doing group by queries, you are no longer interested in each model instances (or in a table row) details, but you want extract new information from your dataset, based on some common aspects shared between the model instances.
Let’s have a look in an example:
class Country(models.Model): name = models.CharField(max_length=30) class City(models.Model): name = models.CharField(max_length=30) country = models.ForeignKey(Country) population = models.PositiveIntegerField()
And the raw data stored in the database:
cities id name country_id population 1 Tokyo 28 36,923,000 2 Shanghai 13 34,000,000 3 Jakarta 19 30,000,000 4 Seoul 21 25,514,000 5 Guangzhou 13 25,000,000 6 Beijing 13 24,900,000 7 Karachi 22 24,300,000 8 Shenzhen 13 23,300,000 9 Delhi 25 21,753,486 10 Mexico City 24 21,339,781 11 Lagos 9 21,000,000 12 So Paulo 1 20,935,204 13 Mumbai 25 20,748,395 14 New York City 20 20,092,883 15 Osaka 28 19,342,000 16 Wuhan 13 19,000,000 17 Chengdu 13 18,100,000 18 Dhaka 4 17,151,925 19 Chongqing 13 17,000,000 20 Tianjin 13 15,400,000 21 Kolkata 25 14,617,882 22 Tehran 11 14,595,904 23 Istanbul 2 14,377,018 24 London 26 14,031,830 25 Hangzhou 13 13,400,000 26 Los Angeles 20 13,262,220 27 Buenos Aires 8 13,074,000 28 Xi'an 13 12,900,000 29 Paris 6 12,405,426 30 Changzhou 13 12,400,000 31 Shantou 13 12,000,000 32 Rio de Janeiro 1 11,973,505 33 Manila 18 11,855,975 34 Nanjing 13 11,700,000 35 Rhine-Ruhr 16 11,470,000 36 Jinan 13 11,000,000 37 Bangalore 25 10,576,167 38 Harbin 13 10,500,000 39 Lima 7 9,886,647 40 Zhengzhou 13 9,700,000 41 Qingdao 13 9,600,000 42 Chicago 20 9,554,598 43 Nagoya 28 9,107,000 44 Chennai 25 8,917,749 45 Bangkok 15 8,305,218 46 Bogotá 27 7,878,783 47 Hyderabad 25 7,749,334 48 Shenyang 13 7,700,000 49 Wenzhou 13 7,600,000 50 Nanchang 13 7,400,000 51 Hong Kong 13 7,298,600 52 Taipei 29 7,045,488 53 Dallas Fort Worth 20 6,954,330 54 Santiago 14 6,683,852 55 Luanda 23 6,542,944 56 Houston 20 6,490,180 57 Madrid 17 6,378,297 58 Ahmedabad 25 6,352,254 59 Toronto 5 6,055,724