如何编写django内部查询sql?

时间:2012-02-21 03:02:02

标签: sql django

如何将sql编写为django model sql语言?

select id,author_id from forum_node as answer_node where node_type='answer' and 
'android' in  (select tagnames from forum_node as question_node where
id=answer_node.parent_id );

任何人都可以提供帮助

1 个答案:

答案 0 :(得分:0)

很难说没有看到模型类,但是选择相关的Forum_Node对象看起来像这样:

Forum_Node.objects.filter(
    node_type="answer", 
    parent__tagnames__name__in=['android']
)

或者,只获取idauthor_id

Forum_Node.objects.filter(
    node_type="answer", 
    parent__tagnames__name__in=['android']
).values_list('id', 'author_id')