Filter on prefetch_related in Django

alan picture alan · Jun 6, 2012 · Viewed 31.4k times · Source

Is there a way of filtering prefetched objects? I need to get the latest() of the prefetched objects but prefetch_related doesn't work if you use latest because the query is changed?

The example here does what I need but I was hoping there's a simpler workaround...

https://github.com/ionelmc/django-prefetch#example

Answer

Webthusiast picture Webthusiast · Jan 20, 2015

As of Django 1.7, filtering prefetched objects is possible. See this SO answer, and the Django documentation.