django query latest of multiple dates

Mohl

I need to find the latest date entry among multiple DateTimeFields.

models.py:

class Presentation(models.Model):
    start = models.DateTimeField(blank=True, null=True, verbose_name=_(u'Start'))
    end = models.DateTimeField(blank=True, null=True, verbose_name=_(u'End'))

I am aware of latest() which sadly only supports querying a single field.

EDIT: I am looking for a one-query solution, if it exists.

ilse2005

For a one query solution you can use django's extra() queryset method:

latest_obj = Presentaion.objects.extra({"latest_date":"greatest(start, end)"}).order_by('latest_date')[0]

This will give you the object with the latest timestamp. latest_obj has now an extra attribute latest_date

latest_obj.latest_date

EDIT:

Using extra() is not recommended and will be deprecated in future versions.

Use this method as a last resort. This is an old API that we aim to deprecate at some point in the future. Use it only if you cannot express your query using other queryset methods.

In django1.9 a database function Greatest was introduced. Now you can use annotate for that:

from django.db.models.functions import Greatest
latest_obj = Presentaion.objects.annotate(last_date=Greatest('start', 'end')).order_by('last_date')[0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

django query latest of multiple dates

From Dev

Query for aceesing latest dates records

From Dev

MySQL query to find latest 3 dates in a table

From Dev

MySQL query to find latest 3 dates in a table

From Dev

Query in elasticsearch with multiple ranges on multiple dates

From Dev

Get latest entry in django in multiple dimensions

From Dev

Django get multiple latest elements using filter

From Dev

single query to get data for multiple dates

From Dev

SQL Query Multiple dates to one date

From Dev

Filter two dates in one query django/python

From Dev

django multiple update dates in one field

From Dev

Not grouping through latest dates

From Dev

mysql SELECT query with multiple joins requiring latest table data

From Dev

mysql SELECT query with multiple joins requiring latest table data

From Dev

Query latest votes per user per post in Django

From Dev

django - query a class by the latest created object of another class

From Dev

Speeding up a Django query for "latest" ForeignKey related object

From Dev

Django ORM: How to query for the latest row based on a another attribute

From Dev

Django Annotated Query to Count Only Latest from Reverse Relationship

From Dev

How to get latest entry among multiple models - django python

From Dev

How to check conflicts on multiple dates from a single query

From Dev

R: how to query from a database multiple times based on different dates

From Dev

lte & gte query inconsistent when Crosschecking for dates in Django

From Dev

How to reverse query objects for multiple levels in django?

From Java

Django REST API - Joining multiple tables in a query

From Dev

Django models multiple query handling exception

From Dev

Django ORM query with multiple inner join

From Dev

Django - How to query and display multiple relationship struture

From Dev

django querying multiple tables - passing parameters to the query

Related Related

  1. 1

    django query latest of multiple dates

  2. 2

    Query for aceesing latest dates records

  3. 3

    MySQL query to find latest 3 dates in a table

  4. 4

    MySQL query to find latest 3 dates in a table

  5. 5

    Query in elasticsearch with multiple ranges on multiple dates

  6. 6

    Get latest entry in django in multiple dimensions

  7. 7

    Django get multiple latest elements using filter

  8. 8

    single query to get data for multiple dates

  9. 9

    SQL Query Multiple dates to one date

  10. 10

    Filter two dates in one query django/python

  11. 11

    django multiple update dates in one field

  12. 12

    Not grouping through latest dates

  13. 13

    mysql SELECT query with multiple joins requiring latest table data

  14. 14

    mysql SELECT query with multiple joins requiring latest table data

  15. 15

    Query latest votes per user per post in Django

  16. 16

    django - query a class by the latest created object of another class

  17. 17

    Speeding up a Django query for "latest" ForeignKey related object

  18. 18

    Django ORM: How to query for the latest row based on a another attribute

  19. 19

    Django Annotated Query to Count Only Latest from Reverse Relationship

  20. 20

    How to get latest entry among multiple models - django python

  21. 21

    How to check conflicts on multiple dates from a single query

  22. 22

    R: how to query from a database multiple times based on different dates

  23. 23

    lte & gte query inconsistent when Crosschecking for dates in Django

  24. 24

    How to reverse query objects for multiple levels in django?

  25. 25

    Django REST API - Joining multiple tables in a query

  26. 26

    Django models multiple query handling exception

  27. 27

    Django ORM query with multiple inner join

  28. 28

    Django - How to query and display multiple relationship struture

  29. 29

    django querying multiple tables - passing parameters to the query

HotTag

Archive