0

Possible Duplicate:
How do you select between two dates with Django

I have django model that contains 2 fields - c_date and c_int. As I am really weak in creating queries... is there some way to fetch from db only those records that fulfill this: c_date+timedelta(days=c_int)=date.today? I would like to do it on DB level because it would be faster than fetching all records and comparing date for all of them.

Community
  • 1
  • 1
middleofdreams
  • 353
  • 3
  • 13

1 Answers1

1

You should use QuerySet.extra for constructing complex WHERE clauses. For your requirement in particular, you should check the availability of the DATEADD function for your database of choice.

Although, depending on how big your data set is and how often you'd need to run this query, another option is to do a migration and simply compute the sum of the date and offset immediately as an instance is created--you can create a custom ModelForm to continue allowing user's to specify c_int as an offset in days.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • That's it! Unfortunatelly in my server there was no DATEADD function but I handled it with simpler SQL query: SELECT * FROM mytable WHERE (c_date+INTERVAL c_int)="2012-08-02"; – middleofdreams Aug 06 '12 at 05:28