Q:

Idiomatic way of counting number of related objects

This is how I am doing it currently:
class Article(models.Model):

    ...

    def nr_notes(self):
        return len(Note.objects.filter(article=self.pk))

class Note(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)

But I thought "there must be a better way". Probably accessing the generated notes_set field?
What is the right way to implement the method nr_notes?

A:

Yes, use count, this does a count query on the database rather than evaluating the filter
 Note.objects.filter(article=self.pk).count()

From evaluating querysets:

A QuerySet is evaluated when you call len() on it. This, as you might expect, returns the length of the result list.
Note: If you only need to determine the number of records in the set (and don’t need the actual objects), it’s much more efficient to handle a count at the database level using SQL’s SELECT COUNT(*). Django provides a count() method for precisely this reason.

