How do I write this Django query in regular SQL?
results = Content.objects.filter(next_decay_at_开发者_JS百科_lte=datetime.datetime.now())
The query that did the ORM of Django is equivalent (Django got the datetime directly from Python) to:
SELECT field1, field2, fieldn FROM app_content WHERE next_decay_at <= NOW()
NOW() is a SQL function that provide the actual datetime. You can get more info at:
http://www.w3schools.com/sql/sql_func_now.asp
You can find out exactly what Django sends with 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.
You should consider leveraging https://github.com/robhudson/django-debug-toolbar and check out the SQL tab. You'll get a chance to see all queries ran within your app.
精彩评论