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
  • 40,873
  • 40
  • 203
  • 237
Brandon Nadeau
  • 3,568
  • 13
  • 42
  • 65

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
  • 32,265
  • 7
  • 79
  • 80
2

You can use raw sql like so:

Thread.objects.raw('SELECT ... FROM myapp_thread WHERE ...')
Ngenator
  • 10,909
  • 4
  • 41
  • 46