← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 4 of 9

Count और Sum से Aggregation

Aggregation हर toy को one by one list करने के बजाय 'मेरे पास total कितने toys हैं?' पूछने जैसा है — यह कई rows को एक single summary number में बदल देता है।
Syntax
markup
from django.db.models import Count, Sum

ModelName.objects.filter(field=value).count()
ModelName.objects.aggregate(total=Sum('field'), number=Count('id'))

Rows Count करना

Matching rows count करने का सबसे simple तरीका QuerySet.count() है, जो हर row को Python में load करने के बजाय database से एक number मांगता है।

Note: count() len(queryset) से तेज़ है क्योंकि यह असली rows fetch नहीं करता।

उदाहरण: Counting Rows

This returns a single integer — the number of published tutorial rows — without loading their data.

markup
from core.models import Tutorial

# How many tutorials are published?
Tutorial.objects.filter(is_published=True).count()
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Count और Sum के साथ aggregate() उपयोग करना

aggregate() पूरी queryset पर एक या ज़्यादा aggregation functions चलाता है और results की एक dictionary return करता है।

उदाहरण: Using aggregate() with Count and Sum

This returns something like {total: 15, total_order: 120} — one dictionary summarizing the whole queryset.

markup
from django.db.models import Count, Sum
from core.models import Chapter

Chapter.objects.aggregate(
    total=Count('id'),
    total_order=Sum('order'),
)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Related Objects Count करना

Count यह भी count कर सकता है कि हर row के कितने related objects हैं, जो 'इस chapter में कितनी tutorials हैं' दिखाने के लिए उपयोगी है।

Warning: Count() एक related field पर सिर्फ तब काम करता है जब एक ForeignKey दोनों models को link करता हो।

उदाहरण: Counting Related Objects

Each Chapter object in the result now has a .tutorial_count attribute showing how many related Tutorial rows point to it.

markup
from django.db.models import Count
from core.models import Chapter

Chapter.objects.annotate(tutorial_count=Count('tutorials'))
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. .count() (एक QuerySet method जो एक number return करता है) को Count (एक aggregation function जो .aggregate() या .annotate() के अंदर उपयोग होता है) के साथ confuse करना।
  2. Count/Sum को django.db.models से उपयोग करने से पहले import करना भूल जाना।
  3. aggregate() उपयोग करना जब annotate() चाहिए था (या इसके उल्टा) — aggregate() पूरी queryset के लिए एक summary return करता है, annotate() हर object में एक value जोड़ता है।
चैप्टर सारांश
  • aggregate() पूरी queryset पर एक single summary value compute करता है।
  • Count और Sum django.db.models से import होते हैं और aggregate() के अंदर उपयोग होते हैं।
  • annotate() एक overall summary के बजाय queryset में हर object में एक computed value जोड़ता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.