Skip to content
Advertisement

converting a Django ValuesListQuerySet object to a float

i have time data saved (in epoch time) in a MySQL database as a IntegerField. i’m doing some data visualisation where i’d like to show orders by the hour and by day of the week as bar charts, so i’d like to convert these epoch times to date time objects and visualise the data accordingly. however, datetime.datetime.fromtimestamp() only takes in floats and i’ve been having issues converting my IntegerField to anything other than a list. (i’ve also tried converting the resulting list to a float, but get a Type Error: float() argument must be a string or a number).

i’m wondering if i should continue to find a way to convert my epoch time (Django ValuesListQuerySet object) in my views.py file, or if i should just return my epoch time as a JsonResponse and do the datetime conversion in my javascript AJAX call (i’m using charts.js to visualise the data). any pointers?

code for reference:

views.py

JavaScript

Advertisement

Answer

You should perform a mapping to datetime objects:

JavaScript
Advertisement