0

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)
4

2 回答 2

2

如果post_set是外键,则使用:

posts = Thread.objects.filter( post_set__created__lt=datetime.date(2013, 5, 10))

如果您仍想使用原始 SQL 查询,请注意这里的详细信息,因为不会自动执行转义

于 2013-05-10T23:58:29.790 回答
2

您可以像这样使用原始 sql

Thread.objects.raw('SELECT ... FROM myapp_thread WHERE ...')
于 2013-05-11T00:01:08.350 回答