imo, `@property` methods on #django models are to be avoided – *especially* when they trigger SQL queries. They seem harmless and convenient, but they're a common gateway to poor performance, model bloat, and hard to undo once baked into areas that typically lack adequate test coverage, like templates.

As an alternative, I ask, can the `@property` method be replaced by a `QuerySet` annotation?

Django

The web framework for perfectionists with deadlines.

Django Project

@RichardBrockie cached property is great for reducing overhead when repeating calls on an instance. The n-query problem when iterating over a queryset remains.

This of course is often solvable with prefetch and/or select related, but it’s not always obvious when accessing a property that there’s a need for this.

@RichardBrockie A classic example of what I consider @property misuse is something like this:

@property
def total_count(self):
return self.other_model.count()

obj.total_count looks innocuous – it feels like a field, but it's costly in an iteration.

A preferred & performant alternative would be: `MyModel.objects.annotate(total_count=Count('other_model'))`.

@marcgibbons I follow you. My inexperienced self left a several of those in my main #Django project. I’ve optimized most of them away with the help of the #djangodebugtoolbar