我是TastyPie
中的新人,我想要一些也许简单任务的帮助(在英语中也是D :)。我有一个字段的两个相关资源。 ForeignKey
我希望得到一个结果集,其中包含一段时间内跑步者执行的总循环。该requeriment反映在下一个SQL查询中。
SELECT r.firstName, r.lastName, COUNT(l.id) FROM Runners r
INNER JOIN Loops l
ON r.id = l.runnerId
WHERE l.start >= '1900-01-01' AND l.end <= '1900-01-02'
Resources.py
class LoopsResource(ModelResource):
operators = fields.ForeignKey("OperatorsResource", "operators", full=True, null=True, blank=True)
class Meta:
queryset = Loopssync.objects.all()
resource_name = 'Loopssync'
authorization = Authorization()
#fields = []
filtering = {
"startedat" : ['gte', 'gt', 'lte', 'lt'],
"finishedat" : ['gte', 'gt', 'lte', 'lt'],
"operatorid": ALL_WITH_RELATIONS
}
class RunnersResource(ModelResource):
class Meta:
queryset = Runners.objects.all()
resource_name = 'Runners'
authorization = Authorization()
fields = ['id', 'firstname', 'lastname']
filtering = {"id": ALL}
Models.py
class Loopssync(models.Model):
remoteid = models.IntegerField(db_column='remoteId', blank=True, null=True)
startedat = models.DateTimeField(db_column='startedAt')
finishedat = models.DateTimeField(db_column='finishedAt')
runnerid = models.ForeignKey('Runners', db_column='runnerId')
class Runners(models.Model):
firstname = models.TextField(db_column='firstName')
lastname = models.TextField(db_column='lastName')
此外,我想知道我应该使用什么URI来在浏览器中获取此设置结果。
答案 0 :(得分:0)
第2行后添加:
runners = fields.ToManyField("RunnersResource", "runners", full=True, null=True, blank=True)
然后你应该能够做到:
?startedat__gte=1900-01-01&finishedat__lte=1900-01-02
没有必要做计数。您可以简单地计算您获得的循环数量。或者也许tastypie甚至会为您提供这些信息。