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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:16:40+00:00 2026-06-09T04:16:40+00:00

So in my Django project, I’ve created a Notification model that has two datetime

  • 0

So in my Django project, I’ve created a Notification model that has two datetime fields, one is a timestamp and one is a due date. The due date is optional. Right now I have a list of notifications, some have a due date and some don’t. They all have a timestamp. What I’m trying to do is to sort them in a list using due date for the notifications that have that field and the timestamp for the notifications that don’t have that optional field. As you can see, it’s kind of weird that I’m using two fields as the key, however they are both datetime fields. So I think they should be able to compare.

my code:

class Notifications(models.Model):
date = models.DateTimeField(auto_now=True)
title = ...
content = ...
due_date = models.DateTimeField(null=True, blank=True)
  • 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-09T04:16:43+00:00Added an answer on June 9, 2026 at 4:16 am

    You want to use an SQL COALESCE:

    The COALESCE function accepts a list of parameters, returning the
    first non-Null value from the list:

    I don’t think there is a django aggregate function for it, but you can do:

    Notifications.objects.filter(**some_filter_args)
      .extra(select={"sortdate" : 'COALESCE("due_date", "date")'})
      .order_by("-sortdate") 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django project that, on one page, has multiple forms (in different
I have a Django project, that has a Address model. This is used in
Model and ModelForm in django project are defined like this : class Consumption(models.Model): date=models.DateTimeField(default=datetime.now)
I maintain a Django project with a database that has several model constraints that
I have Django project on Dreamhost server which has several views that returns Json
I created a django project. It contains a model class with a type attribute
I have a Django project set up with Jenkins. One of my apps has
In my current django project I have a model that stores very long strings
I've got a Django project that has multiple settings files (www site, mobile site,
I have an existing Django project that has several models using concrete inheritance of

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.