I wish to display, in the admin list, a count of the child object collection for that parent item

I am very new to Django, and I have more or less gone through all the tutorial (polls). I have used .net mvc, PHP Larravel, and Node JS to build some pretty big sites on the past, so I am not unfamiliar with using frameworks.

One thing that seems new to me (based on my experience with the other 3 frameworks), is the admin aspect of Django. If I built my own admin (like what I have done with previous frameworks), I likely would not come across this issue. I am mainly practicing at the moment. My app is (I am not looking for a critique on my site!):

Books!

Books have Chapters

Chapters have Paragraphs

In the admin list of books, all I want to do is show the chapter COUNT. All I can manage is to get the list of ALL chapters in the entire database.

I will try as well as I can to only include what is necessary. model.py and admin.py:

#model.py
from django.db import models
from django.db.models import Count #Not used yet!

# Create your models here.
class Book(models.Model):
    title = models.CharField(max_length=255)
    author = models.CharField(max_length=255)
    genre = models.CharField(max_length=200)
    cover_image = models.CharField(max_length=255)
    summary = models.TextField()
    pub_date = models.DateTimeField("date published")
    def __str__(Book):
        return Book.title
    def chapter_count(self):
        return Chapter.objects.count()
    
    
class Chapter(models.Model):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    chapter_image = models.CharField(default='noimage', max_length=255)
    chapter_number = models.IntegerField(default=0)
    def __str__(Chapter):
        return Chapter.title

Specifically note the chapter_count method!

#This is VERY abbreviated! Therefore some code is not shown for brevity reasons:
from django.contrib import admin

from .models import Paragraph, Chapter, Book

#...
class BookAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {"fields": ["title"]}),
        (None, {"fields": ["author"]}),
        (None, {"fields": ["genre"]}),
        (None, {"fields": ["summary"]}),
        (None, {"fields": ["cover_image"]}),
        ("Date information", {"fields": ["pub_date"], "classes": ["collapse"]}),
    ]
    
    inlines = [ChapterInline]
    list_display = [ "title", "author", "chapter_count", ]
#...
#EDIT: thought I better just add this:
admin.site.register(Book, BookAdmin)

That chapter count gives ALL chapters in database.

In either model or admin.py, I would just like to know how to ensure it only shows the chapter_count for that specific Book. I do not know if to do it in model or admin.py

NOTE: Obviously if I know how to do this with Book/ Chapters, I can do exactly the same for Chapters/ Paragraphs.

Sorry for the very long post! Any help would be nice.

Django automatically creates reverse releationships for any Foreign Keys or other relationships.

By default the name of this property is <related_model_name>_set so you method can change to the following:

class Book(models.Model):
    ....
    def chapter_count(self):
        return self.chapter_set.count()

Oh, bravo nanorepublica!

I just knew based on experience with other frameworks this would be elegant and simple. I did so many searches and went to countless stack-overflow posts, but just could not unpack it sensibly to my situation.

I have of course already used that exact notation (chapter_set) in the view template, based on the Polls tutorial (as well .count (without brackets) that I worked out by accident). I think now I have understood that notation for both view (although I should probably say “template-view”, as in MVT as opposed to MVC) and model, it is making much more sense now.