[Fixed]-Getting the count of a Filtered Result in a template on Django

1👍

Django templates are note meant for such complex queries.
There are a few ways you can handle this

One, create a custom django template tag

Two, create a class method which would provide this info.

Example

class Course:
    ...
    def sutdent_assign_count(self):
        #Your query goes here..

0👍

You have far too much logic in the template. Create a method on one of your model classes that actually returns the number you want; I’d propose one, but it’s too unclear what you’re doing (why the .all.0, aren’t the other results important? Why course.course_set, is that a many to many to itself?). What are your models like?

How would you describe what you are displaying in English? That probably gives a hint to what kind of method you should create.

Leave a comment