Greetings to all.
I am working with Django
and I need to obtain a list with the cities ordered by the one with the most inhabitants, here I leave the models that I am using.
class Ciudad(models.Model):
codigo = models.CharField(max_length=4,)
nombre = models.CharField(max_length=128)
class Persona(models.Model):
ciudad_residencia = models.ForeignKey(Ciudad, null=True, blank=True,
verbose_name=_("ciudad de residencia*"), on_delete=models.PROTECT)
The idea is to be able to get the name of the 5 cities with the largest number of people and show the name of the city and next to that the number of people in a table in reporlab
, that's why I store it in a list.
I am using a loop for
but I would like to know if there is a more optimal way to do it, follow the traversal code.
ciudades = Ciudad.objects.all()
personas = Personas.objects.all()
contador_registros = 0
for ciudad in ciudades:
personas_ciudad = personas.filter(ciudad_residencia=ciudad)
if personas_ciudad.count() > 0:
tabla_personas_ciudad.append(
[ciudad.nombre,
personas_ciudad.count()
]
)
contador_registros = contador_registros + 1
if contador_registros >= 5:
break
Also, I don't know how to order it from greater to lesser number of people in the city.
Assuming you want to do the following query:
Django has Aggregation functions , in this case we need the Count, your code would look like this:
Now you want to see the top 5 cities with the largest inhabitants, previously we ordered it from largest to smallest, it only remains to cut the result