如何根据lambda表达式结果过滤Django模型?

时间:2011-05-13 11:51:51

标签: django django-models filter

我知道我可以使用python自己的功能工具集,但我希望在Django中有一种方法。

我有这个型号:

class AssetGeoFenceEvent(models.Model):
#...
#assets, for which this event is assigned
asset = models.ForeignKey(Asset)
#...

,引用的Asset模型如下:

class Asset(models.Model):
#...
client = models.ForeignKey(Client)
#....

我以为我能做到:

#get all the registered events for this client
events = AssetGeoFenceEvent.objects.filter(asset.client == client)

但是失败并出现错误:

  

关键字不能是表达式

试过这个,但它失败了:

#get all the registered events for this client
events = AssetGeoFenceEvent.objects.filter(lambda a: a.client == client)

这给了我一个错误

  

'function'对象不可迭代

那么我怎么能用Django来解决这个问题?

1 个答案:

答案 0 :(得分:6)

You're doing it wrong

events = AssetGeoFenceEvent.objects.filter(asset__client=client)