仅在重新启动 gunicorn 后显示对数据库的更改

Changes to the database are displayed only after restarting gunicorn

prediction_next_list.html模板中NextPredictionListView获取的数据立即更新。而在 LastPredictionsListView (prediction_last_list.html) 后才重启 gunicorn.

class NextPredictionsListView(ListView):
    model = Prediction
    queryset = Prediction.objects.filter(prediction_result=None)
    template_name = 'app/prediction_next_list.html'


class LastPredictionsListView(ListView):
    queryset = Prediction.objects.filter(~Q(prediction_result=None), date__lt=datetime.now())
    template_name = 'app/prediction_last_list.html'

虽然查询集本身是惰性的,但 datetime.now() 的计算不是;将在首次导入 class 时设置。因此,您永远不会看到任何日期晚于该时间的结果。

对于像这样的任何动态,您应该将其移出 class 级别并移至方法中,在本例中为 get_queryset 方法:

class LastPredictionsListView(ListView):
    template_name = 'app/prediction_last_list.html'

    def get_queryset(self):
        return Prediction.objects.filter(~Q(prediction_result=None), date__lt=datetime.now())