I just switched to python and Django-rest-framework from java spring-boot, and I have a question below
Let's say I have some tables:
class table1(CommonModel):
class table2(CommonModel):
class table3(CommonModel):
class table4(CommonModel):
....
And I have an API to return total rows in these tables:
{
"table1": 10,
"table2": 10,
"table3": 10,
"table4": 10,
....
}
My code so far is to count the total on each table with
Model.objects.all().count()
or Model.objects.filter(some_filter).count()
And the problem here is obvious, if I have 10 tables, I will have to call 10 count query, which I think is not a good way to go because it spams too many queries to the database.
I want to limit the number of the query to the database. The best would be to return the row count for all tables in a single query. I have looking for a solution like creating a custom query or so but nothing seems to solve my issue. Edit: I'm using Postgresql database