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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:27:08+00:00 2026-05-26T09:27:08+00:00

I have a simple model to describe a task and a period between executions

  • 0

I have a simple model to describe a task and a period between executions of the task:

class Task(models.Model):
    last_check_timestamp = models.DateTimeField(blank=True, null=True, db_index=True)

class TaskSchedule(models.Model):
    task = models.OneToOneField(Task, blank=False, null=False, related_name='schedule')
    interval_seconds = models.IntegerField(blank=False, null=False)

I want to be able to query, using Django’s ORM, which tasks are fresh and which are stale, meaning those that are due to be run according to their schedule.

I’m currently calculating this for a specific record without the ORM via:

import datetime
task = Task.objects.get(id=123)
fresh = task.last_check_timestamp is not None and datetime.datetime.now() < (task.last_check_timestamp + datetime.timedelta(seconds=task.schedule.interval_seconds))

How would I do the equivalent with Django’s ORM, so I could query all fresh/stale tasks at once?

e.g. This obviously doesn’t work, but something like:

Tasks.objects.filter(Q(last_check_timestamp__isnull=True) || Q(last_check_timestamp + datetime.timedelta(seconds=schedule__interval_seconds)))
  • 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-26T09:27:08+00:00Added an answer on May 26, 2026 at 9:27 am

    I had to do a similar query recently. I was hoping to do it using F() objects, but I don’t think this is possible, because the timedelta relies on a db column (it would be possible if schedule_interval_seconds was a constant).

    In the end, I did the query using the ORM with extra(), and wrote SQL for the where clause.

    sql = "DATE_ADD(last_check_timestamp, INTERVAL schedule.interval_seconds SECOND) < DATE(NOW())"
    Tasks.objects.filter(Q(last_check_timestamp__isnull=True) || Q(last_check_timestamp + datetime.timedelta(seconds=schedule__interval_seconds)))
    tasks = Task.object.select_related('schedule').extra(where=[sql,]
    

    This uses the DATE_ADD function (MySQL docs). You may need to adjust for different flavours of SQL. The select_related is required so that Django does the join to the schedule table.

    The above does not select the tasks where last_check_timestamp is null. I don’t think you can use combine the extra() and your Q object, but you can expand the SQL statement.

    sql = """last_check_timestamp is NULL or DATE_ADD(last_check_timestamp, INTERVAL schedule.interval_seconds SECOND) < DATE(NOW())"""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a simple model class (Part), which pulls from it's information from a
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have a simple model called Party with a corresponding table called parties .
We have a simple domain model: Contact, TelephoneNumber and ContactRepository. Contact is entity, it
I have a simple data model of two tables, email and recipients, email can
I have a simple Poco-Model using abstract classes, and it seems not to work
i have very simple problem. I need to create model, that represent element of
I'm relatively new to the Component Object Model specification - I have a simple
I got a simple problem in SQLAlchemy. I have one model in a table,

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.