Chartjs-If date not in queryset add default value

0👍

Let’s assume your queryset look like that :

obj_tags = [
    {'day': datetime.date(2020, 10, 1), 'tag_occur': 4},
    ...
    {'day': datetime.date(2020, 10, 22), 'tag_occur': 31},
]

You can create an empty list of dict and add the data of the queryset inside:

import datetime

d1 = datetime.date(2020, 10, 1)
d2 = datetime.date(2020, 10, 31)
all_empty_tags = [{'day':d1 + datetime.timedelta(days=x), 'tag_occur': 0} for x in range((d2-d1).days + 1)]

for key, value in enumerate(all_empty_tags):
    try:
        new_tag_occur = [item['tag_occur'] for item in obj_tags if item["day"] == value["day"]][0]
        dd[key]['tag_occur'] = new_tag_occur
    except IndexError:
        pass

You will have all you’r data

Leave a comment