You can use raw sql like so:
Thread.objects.raw('SELECT ... FROM myapp_thread WHERE ...')
سؤال
I am trying to get all the post in a thread before or on a certain time. So how do I get Django to allow me the privilege to enter my own queries?
This is the closest I could come using Django's model functions.
# need to get all the post from Thread post_set that were created before Thread post_set 9
posts = Thread.post_set.filter(created <= Thread.post_set.all()[9].created)
المحلول
You can use raw sql like so:
Thread.objects.raw('SELECT ... FROM myapp_thread WHERE ...')
نصائح أخرى
If post_set
is a foreign key, then use:
posts = Thread.objects.filter( post_set__created__lt=datetime.date(2013, 5, 10))
If you still want to go with a raw SQL query, as detailed here, please be careful, as no escaping is automatically performed.