I’m my Django application I’m fetching all the objects for a particular model like so:
secs = Sections.objects.filter(order__gt = 5)
I pass this varbiles to my templates and i can access all the properties of the Model like section.name, section.id, etc.
There is a model called Books which has a FK to the Sections model. When i iterate over the secs varible in my template, how can i access the count of Books for each Section? Something like {{ sec.Books.count }}??
Thank you
If
Bookshas a ForeignKey toSections, then Django will automatically create a reverse relationship from Sections back to Books, which will be calledbooks_set. This is a Manager, which means you can use.filter(),.get()and.count()on it – and you can use these in your template.(By the way, you should use singular nouns for your model names, not plurals –
Bookinstead ofBooks. An instance of that model holds information for one book, not many.)