Search
 
SCRIPT & CODE EXAMPLE
 

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
"""
Comment

PREVIOUS NEXT
Code Example
Python :: Example of inheritance and constructor in subclass 
Python :: Function argument unpacking in python 
Python :: django url wildcard 
Python :: Get Results From Table Django 
Python :: Generating variations on image data 
Python :: packing a tuple 
Python :: pyqt5 running string and clock stackoverfloww 
Python :: hashmap in python 
Python :: pyPS4Controller usage 
Python :: await not working python 
Python :: python set xticks to int not float 
Python :: How to run smtp4dev as a dotnet global tool 
Python :: Check if a Key is Already Present in a Dictionary 
Python :: Random parola uretme 
Python :: how can i add a list in python 
Python :: how to read file again in python 
Python :: python print statements 
Python :: integrate label into listbox tkinter 
Python :: python scrape data from aspx page 
Python :: cudf - merge dataframes 
Python :: sklearn cheat sheet 
Python :: 218922995834555169026 
Python :: Tape Equilibrium 
Python :: Improve the Request Change User-Agent 
Python :: Python String Membership 
Python :: Python ValueError in strptime() 
Python :: does xgboost accept pandas 
Python :: python check if array alternating 
Python :: python ternary mittels tupel index 
Python :: the most effective search method in python with example 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =