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()
author_count = Author.objects.count()
publisher_count = Publisher.objects.count()
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?
Hi Abraham,
If the models are related, you can use double underscore notation with the Count aggregate function.
If the models are unrelated, then I’m fairly certain that you can only use separate queries to get your results.
--
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...@googlegroups.com.
To post to this group, send email to
django...@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.
To post to this group, send email to djang...@googlegroups.com.
Hi James,
I'm curious... why dropping down from the ORM and doing a single query wouldn't be portable?
I understand that it would happen if you use some kind of stored procedure in the DB, but I guess a single ANSI SQL would do it.
Thanks!
- Alceu
--
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...@googlegroups.com.
To post to this group, send email to django...@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/CA%2Be%2BciVxVrzLuF7ASWiC34A2MenWvJd3P1GXsP%2Bi-mYQ9FuVvg%40mail.gmail.com.
Hi James,
I'm curious... why dropping down from the ORM and doing a single query wouldn't be portable?
I understand that it would happen if you use some kind of stored procedure in the DB, but I guess a single ANSI SQL would do it.
Thanks!
- Alceu
On Wednesday 31 May 2017 12:13:48 'Abraham Varricatt' via Django users wrote:
> 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?
Why is that a concern? This isn't the kind of thing to optimize. Query optimization is about loops and relations. Not about getting information you need - it comes at a price. Have you timed what you could save?
--
Melvyn Sopacua