[英]What is the django equivalent of this SQL query?
I am very new to django and I am tryin to accomplish some repetitive tasks. 我是django的新手,我正尝试完成一些重复性的任务。 I was wondering what would be the django equivalent of the following tasks. 我想知道下面的任务相当于django。
Eg: Let there be a table called exam scores 例如:让表格称为考试成绩
User Physics Chemistry Mathematics Total Date
bill 98 94 Pass 284 02/03/2013
murray 0 89 Fail 89 02/03/2013
bill 10 20 Pass 90 01/29/2013
Now assume I want to write a django command that obtains user bill's object for all cases where he has passed in mathematics. 现在,假设我想编写一个django命令,以获取用户Bill在数学上通过的所有情况的对象。
here I would write something like 在这里我会写类似
scores = Score.objects.filter(user = "bill", Mathematics = "pass")
The problem with the above code is when i do something like 上面的代码的问题是当我做类似的事情时
for s in scores:
print "Physics score =", s.Physics
the control never comes to the print statement. 该控件永远不会出现在print语句中。
So I think you are asking for something like this 所以我想你在问这样的事情
math_scores = Score.objects.filter(user='bill', date__range=(timezone.now() - datetime.timedelta(days=7), timezone.now()).values_list('mathematics', flat=True)
which will give you all math scores for bill in the past week as a list like 这将为您提供过去一周中Bill的所有数学成绩,例如清单
[92, 60]
Is this what you are looking to do or did I misunderstand the question? 这是您要做什么吗?还是我误解了这个问题?
Given the data and query you show above, there are no elements matching 'bill' and 'pass'. 由于数据和查询你看上面有没有匹配的“账单”和“通”的元素。 There are elements matching 'bill' and 'Pass', which is a different thing. 有些元素匹配“账单”和“合格”,这是另一回事。
I don't understand the relevance of your point about date ranges, though. 不过,我不了解您的观点与日期范围的相关性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.