i'm not 100% sure, but doesn't this execute the query ``n`` times (``n`` being the amount of titles with path='')?

Same goes for below. I think you should explicitly execute the ``instance.title_set.all()`` query by casting it to a list just after the if statement above.