2016-07-15 42 views
0

在我看來,我預取相關數據,從模板我可以使用Prefetch_related預取多個模型嗎?爲了

soproduct = SOproduct.objects.select_related('product__material').prefetch_related(
     Prefetch(
      'product__material__bomversion_set', 
      queryset=BOMVersion.objects.default().active(), 
      to_attr='default_active_bomversions' 
     ) 
    ) 

後來讀它,我以前從未用戶預取和計算器上的一個答案得到它。

然而,是否有可能預取多個表?

我還有一個表中的內容,而我也想在同一個模板來顯示這麼近soproduct和BOM的每一個組合我想從Production_order

顯示相關的值
@with_author 
class Production_order(models.Model): 
    version = IntegerVersionField() 
    creation_time = models.DateTimeField(auto_now_add=True, blank=True) 
    BOM = models.ForeignKey(BOM, on_delete=models.PROTECT) 
    soproduct = models.ForeignKey(SOproduct, on_delete=models.PROTECT) 
    agent = models.ForeignKey(User, on_delete=models.PROTECT) 
    quantity_order = models.DecimalField(max_digits=19, decimal_places=3) 
    is_active = models.BooleanField(default=True) 
    is_production= models.BooleanField(default=False) 
    is_prepared = models.BooleanField(default=False) 
    is_picked = models.BooleanField(default=False) 
    production_notes = models.TextField(null=True, blank=True) 
    inventory_notes = models.TextField(null=True, blank=True) 

回答

1

是的,只是間中添加註釋條目。

我從來沒有使用之前,只需要使用prefetch_related預取對象如下:

soproduct = SOproduct.objects.select_related('product__material').prefetch_related(
       'product__material__bomversion_set', 
       'second_table', 
       'third_table__fourth_table' 
       ) 
相關問題