So i have the below's QuerySet output:
<QuerySet [{'price': 12.515, 'price_date': datetime.datetime(2017, 3, 13, 0, 0, tzinfo=)}, {'price': 12.335, 'price_date': datetime.datetime(2017, 3, 14, 0, 0, tzinfo=)}, {'price': 12.37, 'price_date': datetime.datetime(2017, 3, 15, 0, 0, tzinfo=)}, {'price': 12.35, 'price_date': datetime.datetime(2017, 3, 16, 0, 0, tzinfo=)}, {'price': 12.305, 'price_date': datetime.datetime(2017, 3, 17, 0, 0, tzinfo=)}...
How can i get say the price with price_date = 11-26-21 (November 26th 2021)? Or if i want to filter by latest 30 prices?
Thanks
You can use either datetime objects or strings inside the filter expression:
filter_date = datetime.now()
Product.objects.filter(price_date=filter_date)
Product.objects.filter(price_date__lte=filter_date)
# or
Product.objects.filter(price_date__lte="2021-11-27 20:00:00")
Product.objects.filter(price_date="2021-11-27")
For the last 30 items, you would order the query set according to the price_date reversed and then take the first 30 items.
If you already have an initial QuerySet
, you can still filter
your results.
To extract the price, use values_list
prices = qs.filter(price_date='2021-11-26').values_list('price', flat=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.