Django prefetch_related 性能 - to_attr 不适用于嵌套查找

Nub*_*blo 5 python django prefetch

我在 django 项目(v2.1.4)中有一个相对复杂的模型结构。让我们说:

class Pizza(models.Model):
    pass

class ToppingPlacedOnPizza(models.Model):
    # Breaks a many-to-many relationship between Pizza and Topping and stores specific data
    # like the position of the topping in the pizza
    pizza = models.ForeignKey(Pizza, related_name='placed_toppings')
    topping = models.ForeignKey(Topping)
    # position ...

class Topping(models.Model):
    brand = models.ForeignKey(Brand)

class Brand(models.Model):
    name = models.CharField(max_length=32)
Run Code Online (Sandbox Code Playgroud)

我需要检索为一组比萨饼中的每个比萨饼制作配料的品牌的所有名称。说:

pizza1: brand1, brand2, brand3
pizza2: brand2, brand4
...
Run Code Online (Sandbox Code Playgroud)

这样做,prefetch_related对提高性能有很大帮助:

pizzas.prefetch_related('placed_toppings__topping__brand')
Run Code Online (Sandbox Code Playgroud)

但是当查询大约 1000 个比萨时,它开始变得非常慢。根据这个 stackoverflow answer,使用Prefetchclass 并to_attr可以提高性能,但我无法让它在我的情况下工作。to_attr由于嵌套查找,不清楚哪个对象将写入新属性。

我做to_attr了第一级的工作。设置时pizzas.prefetch_related(Prefetch('placed_toppings', to_attr('mytoppings'))),则pizzas[0].mytoppings存在。

但是在设置时pizzas.prefetch_related(Prefetch('placed_toppings__topping__brand', to_attr('mybrands'))),以下都不存在:

pizzas.mybrands
pizzas[0].mybrands
pizzas[0].placed_toppings.mybrands
pizzas[0].placed_toppings.first().mybrands
pizzas[0].placed_toppings.first().topping.mybrands
Run Code Online (Sandbox Code Playgroud)

关于如何提高性能或to_attr处理嵌套查找的任何想法?