Queryset - exclude one data over two

38 views
Skip to first unread message

Stéphane Manguette

unread,
Jan 25, 2019, 9:02:33 AM1/25/19
to Django users
Hello;

I've a DBB with a lot of values which are recorded every 2 minutes (this cannot be changed).

I'm trying to get all values recorded over the last 24h

def dashboard(request):
version = version_global
end_range = datetime.now()
beg_range = end_range - timedelta(days=1)
chart = temp_db.objects.filter(date__gte=beg_range)
    last_entry = chart.latest('date')
return render(request, 'index.html', locals())

The following code works perfectly. The goal is to populate a Chart.js in index.html. Unfortunately, there is a lot to transmit making it long to charge. Since my chart is not mainly meant to show trends; I could skip one data over two. Is there a function do do so? 

Thanks a lot,

Stéphane

C. Kirby

unread,
Jan 25, 2019, 11:07:05 AM1/25/19
to Django users
You can use islice from itertools in this way
islice('ABCDEFG', 0, None, 2) --> A C E G

In your case you would replace
chart = temp_db.objects.filter(date__gte=beg_range)

with
chart = [x for x in islice(temp_db.objects.filter(date__gte=beg_range), 0, None, 2)]


That will give you every other instance from your query set starting with the first

Stéphane Manguette

unread,
Jan 26, 2019, 6:04:37 AM1/26/19
to Django users
Thanks ! It works perfectly !
Reply all
Reply to author
Forward
0 new messages