Django: select_related and GenericRelation

前端 未结 3 444
一个人的身影
一个人的身影 2020-12-09 00:01

Does select_related work for GenericRelation relations, or is there a reasonable alternative? At the moment Django\'s doing individual sql calls for each item in my queryset

3条回答
  •  再見小時候
    2020-12-09 00:31

    you can use .extra() function to manually extract fields :

    Claims.filter(proof__filteryouwant=valueyouwant).extra(select={'field_to_pull':'proof_proof.field_to_pull'})
    

    The .filter() will do the join, the .extra() will pull a field. proof_proof is the SQL table name for Proof model. If you need more than one field, specify each of them in the dictionnary.

提交回复
热议问题