Skip to content Skip to sidebar Skip to footer

How To Get Data From Last 48 Hours - Django

I'm building a news website.I need display 48 hours most viewed news. So I need first to get the 48 hours news, and then get its pv. Currently I'm using a very complicated method

Solution 1:

You can do it with filter, your substract 48 hours from his date created, in case its result is greater than 48 hours or equal, you got recent news

from datetime import datetime, timedelta

thetime = datetime.now() - timedelta(hours=48)
results = news.objects.filter(date_created__gte=thetime)

Note a variable name can't be started with digit : 48_hours_hot_news : WRONG


Post a Comment for "How To Get Data From Last 48 Hours - Django"