TIMEX
TIMEX

Reputation: 271614

How do I write this Django query in regular SQL?

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

Upvotes: 1

Views: 234

Answers (3)

Ben Jackson
Ben Jackson

Reputation: 93690

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.

Upvotes: 1

igalarzab
igalarzab

Reputation: 916

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

Upvotes: 2

nyxtom
nyxtom

Reputation: 2929

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.

Upvotes: 0

Related Questions