在同一个查询集上使用values_list和extra

时间:2013-03-26 23:34:49

标签: django

我有一些在查询集上调用values_list的现有代码。我需要在此查询集中添加额外的东西。如果我在values_list之前调用额外的东西,则在values_list之后的查询中会有额外的东西消失。如果我在调用values_list之后调用额外的东西,它似乎必须生效 - 额外的东西不在查询中。

这是一些调试器输出。在values_list之前额外 - 它就在那里消失了:

-> queryset = queryset.extra(select={field['name'] : field['name']})
(Pdb) n
-> queryset = queryset.values_list(*self.get_search_columns())
(Pdb) print queryset.query
SELECT (top-bottom) AS `top-bottom`,  ....
(Pdb) n
-> data = list(queryset)
(Pdb) print queryset.query
SELECT `data_cst`.`image_measurer_id`, `data_cst`.`image_pr_top_id`,
`data_target`.`name`, `data_recipe`.`name`, `data_cst`.`ep`,
`data_lot`.`name`, `data_cst`.`date_time`, `data_cst`.`bottom` FROM
`data_cst` INNER JOIN `data_target` ON (`data_cst`.`target_name_id` =
`data_target`.`id`) INNER JOIN `data_recipe` ON
(`data_cst`.`recipe_id` = `data_recipe`.`id`) INNER JOIN `data_lot` ON
(`data_cst`.`lot_id` = `data_lot`.`id`) WHERE (`data_cst`.`date_time`
<= 2013-03-26 23:59:59  AND `data_cst`.`date_time` >= 2010-03-26
00:00:00 ) ORDER BY `data_cst`.`date_time` DESC

但是,如果我颠倒了订单,额外的内容根本不会对查询产生影响:

-> queryset = queryset.values_list(*self.get_search_columns())
(Pdb) n
-> queryset = queryset.extra(select={field['name'] : field['name']})
(Pdb) print queryset.query
SELECT `data_cst`.`image_measurer_id`, `data_cst`.`image_pr_top_id`,
`data_target`.`name`, `data_recipe`.`name`, `data_cst`.`ep`,
`data_lot`.`name`, `data_cst`.`date_time`, `data_cst`.`bottom` FROM
`data_cst` INNER JOIN `data_target` ON (`data_cst`.`target_name_id` =
`data_target`.`id`) INNER JOIN `data_recipe` ON
(`data_cst`.`recipe_id` = `data_recipe`.`id`) INNER JOIN `data_lot` ON
(`data_cst`.`lot_id` = `data_lot`.`id`) WHERE (`data_cst`.`date_time`
<= 2013-03-26 23:59:59  AND `data_cst`.`date_time` >= 2010-03-26
00:00:00 ) ORDER BY `data_cst`.`date_time` DESC
{Pdb) n
-> data = list(queryset)
(Pdb) print queryset.query
SELECT `data_cst`.`image_measurer_id`, `data_cst`.`image_pr_top_id`,
`data_target`.`name`, `data_recipe`.`name`, `data_cst`.`ep`,
`data_lot`.`name`, `data_cst`.`date_time`, `data_cst`.`bottom` FROM
`data_cst` INNER JOIN `data_target` ON (`data_cst`.`target_name_id` =
`data_target`.`id`) INNER JOIN `data_recipe` ON
(`data_cst`.`recipe_id` = `data_recipe`.`id`) INNER JOIN `data_lot` ON
(`data_cst`.`lot_id` = `data_lot`.`id`) WHERE (`data_cst`.`date_time`
<= 2013-03-26 23:59:59  AND `data_cst`.`date_time` >= 2010-03-26
00:00:00 ) ORDER BY `data_cst`.`date_time` DESC

有没有办法在同一个查询集上同时使用values_list和extra?

这是一个非常简单的例子,显示它不起作用:

(Pdb) CST.objects.all().extra(select={'ep': 1}).values_list('image_measurer_id')
[(86456L,), (86454L,), (86452L,), (86450L,), (86448L,), (86446L,),
(86444L,), (86442L,), (86440L,), (86438L,), (86436L,), (86434L,),
(86432L,), (86430L,), (86428L,), (86426L,), (86424L,), (86422L,),
(86420L,), (86418L,), '...(remaining elements truncated)...']

这应该每行返回2列。

1 个答案:

答案 0 :(得分:1)

您需要在values_list中包含额外的选择。

E.g。

CST.objects.extra(select={'ep': 1}).values_list('image_measurer_id','ep')