我有以下关系:
class Product(foo):
name = models.CharField()
class Maintenance(foo):
product = models.ForeignKey(Product, related_name="maintenances")
start = models.DateField()
end = models.DateField()
我想使用在给定日期范围内具有start
和end
属性的最新(仅限最新)维护对象来过滤所有产品。
这样的事情:
Product.objects.filter(maintenances__last__end__gte=today.now(), maintenances__last__end__lte=today.now()+datetime.timedelta(days=30))
答案 0 :(得分:1)
您可以{workspace}/flexapp/src/MyApplication.mxml
{workspace}/webapp/WebContent/bin-debug <-- oupput folder of flexapp
所选日期范围内的产品进行维护,然后使用filter
上的annotation
进行最新维护:
Max
答案 1 :(得分:1)
在某些情况下,反过来思考也是有意义的:为每个产品选择最新的Maintenance
对象:
# filter for time range
maintenances = Maintenance.objects.filter(
end__gte=today.now(),
end__lte=today.now() + datetime.timedelta(days=30)
)
# get latest with distinct product id
maintenances = maintenances.order_by(
'product_id', '-end'
).distinct('product_id')
# do a `select_related` to get all products in the same query
maintenances = maintenances.select_related('product')
请注意,只有在使用PostgreSQL时,才能将参数传递给distinct()
。