Wednesday, May 31, 2017

count from multiple tables in a single query?

Hello,

Is it possible to get the count of entries from multiple tables in a single query call? I'm looking at the official docs on aggregation and I can't find anything. For example assume I have the following 2 tables,

class Author(models.Model):
   
name = models.CharField(max_length=100)
   
age = models.IntegerField()

class Publisher(models.Model):
   
name = models.CharField(max_length=300)
   
num_awards = models.IntegerField()

If I want to get the total count from both tables it can be done like this,

author_count = Author.objects.count()
publisher_count
= Publisher.objects.count()

My concern is that this results in two different queries to the database. Can it be done with a single query call?


Yours,
Abraham V.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ffdbc1a-fb84-4bff-a711-eaad77c3ae15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment