当一个相关对象上的字段值小于另一个相关对象上的字段值时,我正在尝试向QuerySet添加一个True / False的注释。
以下是一些示例的模型:
class RobotManager(models.Manager):
queryset = super(RobotManager, self).get_queryset()
queryset = queryset.annotate(canteen_empty=UNKNOWN CODE)
return queryset
class Robot(models.Model):
# Has some other unrelated stuff
objects = RobotManager()
class CanteenLevel(models.Model):
time = models.DateTimeField()
robot = models.ForeignKey("SomeApp.Robot")
gallons = models.IntegerField()
class RobotConfiguration(models.Model):
time = models.DateTimeField()
robot = models.ForeignKey("SomeApp.Robot")
canteen_empty_level = models.IntegerField()
使用上述模型,随着机器人配置或CanteenLevel的变化,我们创建新记录并保存历史记录。
我想做的是向Robot QuerySet添加一个注释,指出Robot的食堂是否为空(Robot的最新CanteenLevel.gallons小于Robot的最新Configuration.canteen_empty_level)。
目的是使用QuerySet中的注释允许这样的语句:
bad_robots = Robot.objects.filter(canteen_empty=True)
我在注释中尝试过类似的东西:
canteen_empty=ExpressionWrapper(CanteenLevel.objects.filter(robot=OuterRef('pk')).order_by('-time').values('gallons')[:1] <= RobotConfiguration.objects.filter(robot=OuterRef('robot')).order_by('-time').values('canteen_empty_level')[:1], output_field=models.BooleanField))
但显然不允许使用“<=”运算符。
我也试过这个:
canteen_empty=Exists(CanteenLevel.objects.filter(robot=OuterRef('pk')).order_by('-time').values('gallons')[:1].filter(gallons__lte=Subquery(RobotConfiguration.objects.filter(robot=OuterRef('robot')).order_by('-time').values('canteen_empty_level')[:1]))))
但是在获取QuerySet片段后无法进行过滤。
任何帮助,将不胜感激!
发布于 2019-06-13 16:27:43
我们可以在这里做两个注释:
from django.db.models import Subquery, OuterRef
latest_gallons = Subquery(CanteenLevel.objects.filter(
robot=OuterRef('pk')
).order_by('-time').values('gallons')[:1])
latest_canteen = Subquery(RobotConfiguration.objects.filter(
robot=OuterRef('pk')
).order_by('-time').values('canteen_empty_level')[:1])
然后我们可以先Robot
用这些注释对象,并过滤:
from django.db.models import F
Robot.objects.annotate(
latest_gallons=latest_gallons,
latest_canteen=latest_canteen
).filter(latest_gallons__lte=F('latest_canteen'))
这将构造一个如下所示的查询:
SELECT robot.*,
(SELECT U0.gallons
FROM canteenlevel U0
WHERE U0.robot_id = robot.id
ORDER BY U0.time DESC
LIMIT 1) AS latest_gallons,
(SELECT U0.canteen_empty_level
FROM robotconfiguration U0
WHERE U0.robot_id = robot.id
ORDER BY U0.time DESC
LIMIT 1) AS latest_canteen
FROM robot
WHERE
(SELECT U0.gallons
FROM canteenlevel U0
WHERE U0.robot_id = robot.id
ORDER BY U0.time DESC
LIMIT 1
) <= (
SELECT U0.canteen_empty_level
FROM robotconfiguration U0
WHERE U0.robot_id = robot.id
ORDER BY U0.time DESC
LIMIT 1
)
但请注意,如果a Robot
没有相关CanteenLevel
或RobotConfiguration
(其中一个或两者),那么Robot
它将不会包含在查询集中。
https://stackoverflow.com/questions/-100007015
复制相似问题