Django: Force select related?

前端 未结 3 1634
情歌与酒
情歌与酒 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:44

    You can create a custom manager, and simply override get_queryset for it to apply everywhere. For example:

    class MyManager(models.Manager):
        def get_queryset(self):
            return super(MyManager, self).get_queryset().select_related('foo', 'bar')
    

    (Prior to Django 1.6, it was get_query_set).

提交回复
热议问题