Django ORM: Selecting related set

后端 未结 4 1076
遥遥无期
遥遥无期 2020-12-24 13:51

Say I have 2 models:

class Poll(models.Model):
    category = models.CharField(u\"Category\", max_length = 64)
    [...]

class Choice(models.Model):
    pol         


        
4条回答
  •  囚心锁ツ
    2020-12-24 14:20

    I think what you are trying to do is the term "eager loading" of child data - meaning you are loading the child list (choice_set) for each Poll, but all in the first query to the DB, so that you don't have to make a bunch of queries later on.

    If this is correct, then what you are looking for is 'select_related' - see https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related

    I noticed you tried 'select_related' but it didn't work. Can you try doing the 'select_related' and then the filter. That might fix it.


    UPDATE: This doesn't work, see comments below.

提交回复
热议问题