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

def charts(request):  # pie chart for payment type
    dow_queryset = Member.objects.order_by('member_created_time').values_list(
        'member_created_time', flat=True) # return only the created time w/o key
    dow_queryset = list(dow_queryset) # list is the only native data type i can convert to successfully
    dow_queryset = float(dow_queryset) # this just .. doesn't work, lol
    
    print(type(dow_queryset))

    hkt = pytz.timezone('Asia/Shanghai')
    dt_obj = hkt.localize(datetime.datetime.fromtimestamp(dow_queryset)) # this line doesn't work as datetime.datetime.fromtimestamp() only takes in a float, whereas `dow_queryset` is either a list/ Django ValuesListQuerySet object 

    
    """
    data - rides by the hour
    """
    hour_count = {}

    for obj in dt_obj:
        if obj.hour == 0:
            hour_count['midnight'] += 1
        elif obj.hour < 6:
            hour_count['early_morning'] += 1
        elif obj.hour < 12:
            hour_count['morning'] += 1
        elif obj.hour < 19:
            hour_count['afternoon'] += 1
        else:
            hour_count['night'] += 1
    
    """
    data - rides by weekday (bar chart)
    """
    weekday_count = {}

    for obj in dt_obj:
        if obj.weekday() == 0:
            weekday_count['monday'] += 1
        elif obj.weekday() == 1:
            weekday_count['tuesday'] += 1
        elif obj.weekday() == 2:
            weekday_count['wednesday'] += 1
        elif obj.weekday() == 3:
            weekday_count['thursday'] += 1
        elif obj.weekday() == 4:
            weekday_count['friday'] += 1
        elif obj.weekday() == 5:
            weekday_count['saturday'] += 1
        elif obj.weekday() == 6:
            weekday_count['tuesday'] += 1
    
    return JsonResponse({
        'labels_day' : list(weekday_count.keys()),
        'data_day': list(weekday_count.values()),
        'labels_hour': list(hour_count.keys()),
        'data_hour': list(hour_count.values()),
    })

Advertisement

Answer

You should perform a mapping to datetime objects:

dow_queryset = Member.objects.order_by('member_created_time').values_list(
        'member_created_time', flat=True
)
hkt = pytz.timezone('Asia/Shanghai')
dt_obj = [hkt.localize(datetime.datetime.fromtimestamp(x)) for x in dow_queryset]
Advertisement