0

我有一些在查询集上调用 values_list 的现有代码。我需要在这个查询集中使用额外的东西。如果我在 values_list 之前调用 extra,那么额外的东西就会从 values_list 之后的查询中消失。如果我在调用 values_list 之后调用 extra ,它似乎必须生效 - 额外的东西不在查询中。

这是一些调试器输出。在 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 列。

4

1 回答 1

1

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

例如

CST.objects.extra(select={'ep': 1}).values_list('image_measurer_id','ep')
于 2013-03-27T13:29:16.653 回答