有没有办法打乱查询集,使其每天显示不同的值?

Is there a way to shuffle a queryset so it shows a different value each day?

我正在构建一个 Django 2.5 网络应用程序,每天显示来自历史人物的不同引述。问题是这个报价对于每个用户来说应该是不同的,并且应该每天更改而不重复相同的报价。

我已经做了一些研究,唯一接近的是在我请求查询集时使用偏移量,但这并不能解决每个用户的随机引用问题。

quotes/views.py
import datetime

...

now = datetime.datetime.now()
today_tt = now.timetuple()
julian_day = today_tt.tm_yday
#asuming there are 365 quotes on the database
quotes.objects.all()[jualian_day]

我可以做些什么来实现这个?

import random

# TODO Retrieve into pks_used the previously saved primary keys 
# of the quotes already displayed to the current user
pks_used = set()

# Get the primary keys of all quotes
all_pks = set(quotes.objects.values_list('pk', flat=True))

# Exclude the quotes that have been shown already
eligible_pks = all_pks - pks_used

# Get a random quote from the list of unused quotes
# random.sample returns a list 
# (with just one element in our case)
rnd_pk = random.sample(eligible_pks, 1)[0]
todays_quote = quotes.objects.get(pk=rnd_pk)

# TODO display the quote and store its private key in the list of 
# already displayed quotes