django can we select related a field on a prefetch related model?

后端 未结 2 755
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 08:19

Assuming these as django model for the sake of simplcity:

class A():

    a = manytomany(\'B\')

class B():

    b = charfield()
    z = foreignkey(\'C\')

class         


        
2条回答
  •  清歌不尽
    2021-01-31 09:12

    You only need one prefetch_related call:

    foo = A.objects.prefetch_related('a__z').get(pk=1)
    

    This will prefetch both tables. In Django 1.7+ you can improve performance by using a Prefetch object, as in koniiiik's answer.

提交回复
热议问题