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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:30:02+00:00 2026-05-30T10:30:02+00:00

In my Django model, I’ve got a very simple model which represents a single

  • 0

In my Django model, I’ve got a very simple model which represents a single occurrence of an event (such as a server alert occurring):

class EventOccurrence:
    event = models.ForeignKey(Event)
    time = models.DateTimeField()

My end goal is to produce a table or graph that shows how many times an event occurred over the past n weeks.

So my question has two parts:

  • How can I group_by the week of the time field?
  • How can I “pad out” the result of this group_by to add a zero-value for any missing weeks?

For example, for the second part, I’d like transform a result like this:

| week | count |                   | week | count |
| 2    | 3     |                   | 2    | 3     |
| 3    | 5     |   —— becomes —>   | 3    | 5     |
| 5    | 1     |                   | 4    | 0     |
                                   | 5    | 1     |

What’s the best way to do this in Django? General Python solutions are also OK.

  • 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-30T10:30:05+00:00Added an answer on May 30, 2026 at 10:30 am

    Django’s DateField as well as datetime doesn’t support week attribute. To fetch everything in one query you need to do:

    from django.db import connection
    
    cursor = connection.cursor()
    cursor.execute(" SELECT WEEK(`time`) AS 'week', COUNT(*) AS 'count' FROM %s GROUP BY WEEK(`time`) ORDER BY WEEK(`time`)" % EventOccurrence._meta.db_table, [])
    
    data = []
    results = cursor.fetchall()
    for i, row in enumerate(results[:-1]):
        data.append(row)
    
        week = row[0] + 1
        next_week = results[i+1][0]
        while week < next_week:
            data.append( (week, 0) )
            week += 1
    data.append( results[-1] )
    
    print data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Django model called Person, which has got a 'user' ForeignKey
I have the following Django Model which retrieves 3 records from a database. The
I am making a Django Model decorator which takes a Django model and gives
I have a Django model, which is essentially a list of words. It is
I've got a django model that contains a manytomany relationship, of the type, class
I have Django Model with many fields which user must fill. If I'll create
I have a Django model that contains a date field: class Event(models.Model): event_date =
I have a django model, which has a int field (with null=True, blank=True). Now
I have a Django model for Cat which has a foreign key Dog .
My Django model has a field within which I want to have reference logic.

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.