我有4个模特:
class TransitLine(models.Model):
name = models.CharField(max_length=32)
class Stop(models.Model):
line = models.ForeignKey(TransitLine, related_name='stops')
class ExitType(models.Model):
name = models.CharField(max_length=32)
people_limit = models.PositiveSmallIntegerField()
class Exits(models.Model):
TOKEN_BOOTH = 0
GATE = 1
REVOLVING_DOOR = 2
EXIT_TYPES = (
(TOKEN_BOOTH, 'Token booth'),
(GATE, 'Gate'),
(REVOLVING_DOOR, 'Revolving door'),
)
name = models.CharField(max_length=32)
stop = models.ForeignKey(Stop, related_name='exits')
type = models.ForeignKey(ExitType, related_name='exits')
我有一个TransitLine
个对象。我想要检索与ExitType
的{{1}}个对象相关的所有唯一Stop
个对象(这是一个满嘴)。
我想做的一些半伪代码:
TransitLine
显然,更喜欢在一次tl = TransitLine.objects.get(id=1)
exit_types = []
for s in tl.stops:
exit_types.append([e.type for e in s.exits])
unique_exit_types = list(set(exit_types))
电话中执行此操作。有什么建议吗?
答案 0 :(得分:2)
我会尝试这样的事情:
ExitType.objects.filter(exits__stop__line=line).distinct()