Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:09:04+00:00 2026-06-04T05:09:04+00:00

I have the following model: Deal(models.Model): start_date = models.DateTimeField() end_date = models.DateTimeField() I want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:09:06+00:00Added an answer on June 4, 2026 at 5:09 am

    How can I iterate through a given year?

    You could use python-dateutil’s rrule. Install with command pip install python-dateutil.

    Example usage:

    In [1]: from datetime import datetime
    
    In [2]: from dateutil import rrule
    
    In [3]: list(rrule.rrule(rrule.MONTHLY, dtstart=datetime(2010,01,01,00,01), count=12))  
    Out[3]: 
    [datetime.datetime(2010, 1, 1, 0, 1),
     datetime.datetime(2010, 2, 1, 0, 1),
     datetime.datetime(2010, 3, 1, 0, 1),
     datetime.datetime(2010, 4, 1, 0, 1),
     datetime.datetime(2010, 5, 1, 0, 1),
     datetime.datetime(2010, 6, 1, 0, 1),
     datetime.datetime(2010, 7, 1, 0, 1),
     datetime.datetime(2010, 8, 1, 0, 1),
     datetime.datetime(2010, 9, 1, 0, 1),
     datetime.datetime(2010, 10, 1, 0, 1),
     datetime.datetime(2010, 11, 1, 0, 1),
     datetime.datetime(2010, 12, 1, 0, 1)]
    

    Use the month to do a query?

    You could iterate over months like this:

    In [1]: from dateutil import rrule
    
    In [2]: from datetime import datetime
    
    In [3]: months = list(rrule.rrule(rrule.MONTHLY, dtstart=datetime(2010,01,01,00,01), count=13))
    
    In [4]: i = 0
    
    In [5]: while i < len(months) - 1:
       ...:         print "start_date", months[i], "end_date", months[i+1]
       ...:         i += 1
       ...:     
    start_date 2010-01-01 00:01:00 end_date 2010-02-01 00:01:00
    start_date 2010-02-01 00:01:00 end_date 2010-03-01 00:01:00
    start_date 2010-03-01 00:01:00 end_date 2010-04-01 00:01:00
    start_date 2010-04-01 00:01:00 end_date 2010-05-01 00:01:00
    start_date 2010-05-01 00:01:00 end_date 2010-06-01 00:01:00
    start_date 2010-06-01 00:01:00 end_date 2010-07-01 00:01:00
    start_date 2010-07-01 00:01:00 end_date 2010-08-01 00:01:00
    start_date 2010-08-01 00:01:00 end_date 2010-09-01 00:01:00
    start_date 2010-09-01 00:01:00 end_date 2010-10-01 00:01:00
    start_date 2010-10-01 00:01:00 end_date 2010-11-01 00:01:00
    start_date 2010-11-01 00:01:00 end_date 2010-12-01 00:01:00
    start_date 2010-12-01 00:01:00 end_date 2011-01-01 00:01:00
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
Say I have the following model: class Person(models.Model): street_address = models.CharField(max_length=50, blank=True) suburb =
I have following model for which i want to update team which is a
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following model association: a student model and has_many scores. I need
I have the following model setup - a User is interested in projects in
I have the following Model and Controller files, and when i visit this url,
I have the following model associations: class Slider < ActiveRecord::Base has_one :featured_happening, :as =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.