简体   繁体   English

如何在常规SQL中编写此Django查询?

[英]How do I write this Django query in regular SQL?

results = Content.objects.filter(next_decay_at__lte=datetime.datetime.now())

The query that did the ORM of Django is equivalent (Django got the datetime directly from Python) to: 执行Django ORM的查询等效于Django(Django直接从Python获取日期时间):

SELECT field1, field2, fieldn FROM app_content WHERE next_decay_at <= NOW()

NOW() is a SQL function that provide the actual datetime. NOW()是一个提供实际日期时间的SQL函数。 You can get more info at: 您可以在以下位置获取更多信息:

http://www.w3schools.com/sql/sql_func_now.asp http://www.w3schools.com/sql/sql_func_now.asp

You can find out exactly what Django sends with results._as_sql() . 您可以准确找到Django发送的results._as_sql() In your particular case it's going to write a query with the specific date you passed in. If you were really writing the query directly in SQL you'd probably want to use a symbolic NOW() instead. 在您的特定情况下,它将使用传递的特定日期来编写查询。如果您实际上是直接在SQL中直接编写查询,则可能要使用符号NOW()

You should consider leveraging https://github.com/robhudson/django-debug-toolbar and check out the SQL tab. 您应该考虑利用https://github.com/robhudson/django-debug-toolbar并检查“ SQL”选项卡。 You'll get a chance to see all queries ran within your app. 您将有机会看到所有查询在您的应用程序中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM