I have the following model:
Deal(models.Model):
start_date = models.DateTimeField()
end_date = models.DateTimeField()
I want to iterate through a given year
year = '2010'
For each month in year I want to execute a query to see if the month is between start_date and end_date.
How can I iterate through a given year? Use the month to do a query?
SELECT * FROM deals WHERE month BETWEEN start_date AND end_date
The outcome will tell me if I had a deal in January 2010 and/or in February 2010, etc.
You could use python-dateutil’s rrule. Install with command
pip install python-dateutil.Example usage:
You could iterate over months like this:
Replace the “print” statement with a query. Feel free to adapt it to your needs.
There is probably a better way but that could do the job.