Django: Force select related?

前端 未结 3 1627
情歌与酒
情歌与酒 2021-02-01 02:09

I\'ve created a model, and I\'m rendering the default/unmodified model form for it. This alone generates 64 SQL queries because it has quite a few foreign keys, and those in tur

3条回答
  •  生来不讨喜
    2021-02-01 02:43

    Create a custom models.Manager and override all the methods (filter, get etc.) and append select_related onto every query. Then set this manager as the objects attribute on the model.

    I would recommend just going through your code and adding the select_related where needed, because doing select_related on everything is going to cause some serious performance issues down the line (and it wouldn't be entirely clear where it's coming from).

提交回复
热议问题