current_year = timezone.now().year Question.objects. get (pub_date__year=current_year) # 等价于(嵌套子查询) select question_text from polls_question where year ( pub_date ) =( select year ( curdate( ))) ;
Choice.objects.filter(question__pub_date__year=current_year) # 等价于 select choice_text from polls_choice as c join polls_question as q on c.question_id=q. id where year ( q.pub_date ) =( select year ( curdate( ))) ;
模糊查询并删除记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
q.choice_set. filter (choice_text__startswith= 'Just hacking' ). delete () # 等价于 delete from polls_choice where choice_text like 'Just hacking%' ;
查询并排序
1 2 3 4 5 6 7 8 9 10 11 12
# 这里的-我没明白什么意思[TODO] Question.objects.order_by( '-pub_date' ) # 等价于 select question_text from polls_question order by pub_date;