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)
Kevin Brown-Silva
  • 39,112
  • 40
  • 199
  • 230
Brandon Nadeau
  • 3,328
  • 12
  • 40
  • 60

2 Answers2

2

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.

Stefano Sanfilippo
  • 30,653
  • 7
  • 76
  • 79
2

You can use raw sql like so:

Thread.objects.raw('SELECT ... FROM myapp_thread WHERE ...')
Ngenator
  • 10,479
  • 4
  • 39
  • 45