【发布时间】:2013-04-10 01:45:48
【问题描述】:
我有这两个模型(简化):
class Place(OrderedModel):
name = models.CharField(max_length=100)
class Team(models.Model):
name = models.CharField(max_length=100)
places = models.ManyToManyField(Place, blank=True)
假设有十个 Place 实例。但只有五个Team 实例。我想按Place 中的条目顺序返回一个布尔值列表,其中True 表示Team.places 包含该位置,而False(显然)表示它不包含。 Team.places 字段没有特定的顺序。
在以下示例中,Team 具有前四个 Place 对象的实例,最后一个对象位于其 places 字段中:
[True, True, True, True, False, False, False, False, False, True]
我在 Team 模型上用这个方法解决了这个问题:
class Team(models.Model):
...
def done(self):
"""
Returns a list of Booleans, one for each place
in order. True means that the team has found the
place. False, that they have not.
"""
places = Place.objects.all()
return [p in self.places.all() for p in places]
它可以工作,但似乎效率很低。它进行(我相信)两个不同的 SQL 查询,然后是列表理解。有没有更有效的方法来解决这个问题?
已解决
我最终这样做了:
def done(self):
"""
Returns a list of Booleans, one for each place
in order. True means that the team has found the
place. False, that they have not.
"""
sql = '''SELECT P.id, (TP.id IS NOT NULL) AS done
FROM qrgame_place P
LEFT OUTER JOIN qrgame_team_places TP
ON P.id = TP.place_id AND TP.team_id = %s'''
places = Place.objects.raw(sql, [self.id])
for p in places:
yield bool(p.done)
【问题讨论】:
标签: python django optimization