Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to get total seconds in django queryset for timedelta field

"""
Sure can, but the exact mechanism may depend upon your database.

In postgres, you can use EXTRACT(epoch FROM <interval>) to get the total number of seconds.

To use this in Django, you can create a Func subclass:

"""

class Epoch(django.db.models.expressions.Func):
    template = 'EXTRACT(epoch FROM %(expressions)s)::INTEGER'
    output_field = models.IntegerField()
    
#Then you can use it directly:

base_posts.annotate(
    response_time_sec=Epoch(F('min_comment_date') - F('date_created'))
)


"""
reference:
https://stackoverflow.com/questions/63905443/django-queryset-return-durationfield-value-in-seconds
"""
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #total #seconds #django #queryset #timedelta #field
ADD COMMENT
Topic
Name
3+7 =