[Django]-Django: ValueError: Prefetch querysets cannot use raw(), values(), and values_list()

3👍

You can indeed not make use of .values(…) [Django-doc] and .values_list(…) [Django-doc] when prefetching. That would also result in extra complications, since then Django has more trouble to find out how to perform the "joining" at the Django/Python level, if this is even possible.

You however do not need to use .values(…) to limit the number of columns you fetch, you can use .only(…) [Django-doc] instead. .only(…) will only fetch the given columns and the primary key immediately in memory, and will "postpone" fetching other columns. Only if you later need these columns, it will make extra queries. This is thus a bit similar how Django will only fetch the related object of a ForeignKey in memory when you need it with an extra query.

queryset = SymbolList.objects.prefetch_related(
    Prefetch(
        'stock_dailypricehistory_symbol',
        queryset = DailyPriceHistory.objects.filter(
            id__in=[1,2,3,4]
        ).only('close', 'volume', 'symbol_id')
    )
)

The symbol_id is the column of the ForeignKey that refers from the DailyPriceHistory to the SymbolList model, this might be a different field than symbol_id. If you do not include this, Django will have to make extra queries to find out how it can link DailyPriceHistory to the corresponding SymbolList, and this will only slow down processing with extra queries, therefore you better include this as well.

Leave a comment