I have a todo model defined below:
class Action(models.Model):
name = models.CharField(\"Action Name\", max_length=200, unique = True)
complete = mo
It's possible to use prefetch_related
to retrieve the tags, but you need to sidestep around the 'tags' property, since - as jdi says - this is a custom manager rather than a true relation. Instead, you can do:
actions = Action.objects.select_related('reoccurance').filter(complete=False)\
.prefetch_related('tagged_items__tag')
Unfortunately, action.tags.all
in your template code will not make use of the prefetch, and will end up doing its own query - so you need to take the rather hacky step of bypassing the 'tags' manager there too:
{% for tagged_item in action.tagged_items.all %}
{{ tagged_item.tag }}{% if not forloop.last %}, {% endif %}
{% endfor %}
(Ed.: if you're getting "'QuerySet' object has no attribute 'prefetch_related'", that suggests that you're on a version of Django below 1.4, where prefetch_related isn't available.)