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 811697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:05:08+00:00 2026-05-15T01:05:08+00:00

I have been lurking and learning in here for a while. Now i have

  • 0

I have been lurking and learning in here for a while. Now i have a problem that somehow i cannot see an easy solution. In order to learn django i am bulding an app that basically keeps track of booked items.
What I would like to do is to show how many days per month for a selected year one item has been booked.

i have the following models:

Asset(Model)

BookedAsset(Model):
 asset = models.ForeignKey(Asset)
 startdate = models.DateField()
 enddate = models.DateField()

So having the following entries:

asset 1, 2010-02-11, 2010-02-13
asset 2, 2010-03-12, 2010-03-14
asset 1, 2010-04-30, 2010-05-01

I would like to get returned the following:

asset 1    asset 2
-------    -------
Jan = 0    Jan = 0
Feb = 2    Feb = 0
Mar = 0    Mar = 2
Apr = 1    Apr = 0
May = 1    May = 0
Jun = 0    Jun = 0
Jul = 0    Jul = 0
Aug = 0    Aug = 0
Sep = 0    Sep = 0
Oct = 0    Oct = 0
Nov = 0    Nov = 0
Dec = 0    Dec = 0

I know i need to first get the number of days in a date range (and keep track if they fall out of the current month and into the next month) and then do an agregate on the number of days. I am just stuck on how to do it elegantly in Django.

Any help (or hint in the right direction) is greatly appreciated.

  • 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-05-15T01:05:09+00:00Added an answer on May 15, 2026 at 1:05 am

    At the end I went with a custom query as a solution to my question:

    cursor = connection.cursor()
    cursor.execute("""select to_char(allmonths.yeardate::date, 'YYYY/MM/DD') as monthdate,
                        COUNT("day") as bookings
                        from (
                        select distinct date_trunc('month', (date %s - offs)) as yeardate 
                        from generate_series(0,365,28) as offs
                        ) as allmonths
                        left join bookings_bookedassetday 
                        on EXTRACT(MONTH from "day")=EXTRACT(MONTH from yeardate) 
                        and asset_id=%s and EXTRACT(YEAR from "day") = %s
                        group by allmonths.yeardate
                        order by allmonths.yeardate asc""", [year+'-12-31', id, year])
    
    query = cursor.fetchall()
    

    Maybe it is not the most django-ist way of doing it but I found it way easier than figuring it out with pure django 😐

    If somebody else has a better alternative I am all ears 🙂

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

Sidebar

Related Questions

I have been looking for a solution to this problem for a while now
Have been trying to fix this problem I have here. Basically I have a
Have been looking at the MVC storefront and see that IQueryable is returned from
First post, but I've been lurking around this site for a long while now,
Have been trying to encrypt an xml file to a string so that I
I have been working with SQL Server as a Developer a while. One thing
have been trying couple of hours now to make my iphone app universal. The
Have been searching the net for the past hours to find a solution to
Have been working on a Tower Defense game for iOS for some time now.
I have been lurking for a long time on this forum and I found

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.