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

  • Home
  • SEARCH
  • 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 4267380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:53:30+00:00 2026-05-21T06:53:30+00:00

Say I have a model that looks like: class Action(models.Model): user = models.ForeignKey(‘auth.User’) action

  • 0

Say I have a model that looks like:

class Action(models.Model):
    user = models.ForeignKey('auth.User') 
    action = models.IntegerField(choices=ACTION_CHOICES)
    when = models.DateTimeField()

I want to get all of the Actions for a user grouped by them being within 15 minutes of each other. Preferably in django’s ORM if possible but if there is no other way, then PostgreSQL answers wouldn’t be bad.

More clarification:

What I am looking for is along the lines of if a and b are 13 minutes apart, b and c are 10 minutes apart, a b and c would be grouped together even though a and c are 23 minutes apart.

  • 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-21T06:53:30+00:00Added an answer on May 21, 2026 at 6:53 am

    From your example, I’m assuming each subsequent action will be within 15 minutes of the last one in a sorted list of actions:

    # sort by when in ascending order
    actions = Action.objects.order_by('when')
    
    group_dict = {}
    action_list = []
    
    count = 0
    for action in actions:
        if action_list:
            diff = action.when - action_list[-1].when
            if diff.seconds <= 900: # 15 minutes in seconds
                action_list.append(action)
            else:
                group_dict[count] = action_list
                action_list = [action]
                count += 1
        else:
            action_list.append(action)
    
    print group_dict
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: class ProjectImage(models.Model): big_thumb = ThumbnailField(upload_to='profiles', size=(500,
Let's say that you have a Model that looks kind of like this: public
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Say I have the following in my models.py : class Company(models.Model): name = ...
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
Say that I have two models- Users and Accounts. Each account can have at
Let's say I have two models, Classes and People. A Class might have one
I have a model that has an array of Strings called tags, public class
Let's say I have this model named Product with a field named brand .

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.