Django inline formsets and choicefields generate too many db queries

前端 未结 2 1649
独厮守ぢ
独厮守ぢ 2021-02-09 12:28

I have a model with a number of foreign key fields, e.g. model Product with fields \'type\', \'level\', \'color\', \'intensity\' (just a generic example).

I then have a

相关标签:
2条回答
  • 2021-02-09 12:54
    field_queryset = Test.objects.all()    
    for form in formset:
            form.fields['test_field'].queryset = field_queryset
    

    Like this.

    0 讨论(0)
  • 2021-02-09 13:02

    You can change the queryset used by the formset, then you could use select_related() to generate the FK joins rather than to execute a query at each forloop iteration.

    0 讨论(0)
提交回复
热议问题