1👍
MySQL has a table named information_schema.tables
that contains the tables as records, and has table_rows
.
You can make a non-managed model and then filter with:
class Table(models.Model):
table_name = models.CharField(max_length=128)
table_rows = models.PositiveIntegerField()
class Meta:
managed = False
db_table = 'information_schema.tables'
We can then obtain the number of records for User
and Group
for example with:
from django.contrib.auth.models import Group, User
Table.objects.filter(table_name__in=[User._meta.db_table, Group._meta.db_table])
By subclassing the CharField
for the table_name
field, we could even make the interface more convenient.
The main advantage is that you can fetch the counts in "bulk": retrieving the count of for example twenty models, without the need to perform twenty queries.
Source:stackexchange.com