我有这两个模型(简化):
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)
假设有 10 个实例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)