Let’s play ‘Spot the bug’. We’re building a simple system that shows photos. Each photo has a
publish_date and we should only show photos that have been published (i.e. their
in the past).
1 2 3 4 5
And the view to show those photos:
Can you spot the bug? I sure didn’t… until the client complained that newly published photos never showed up on the site. Restarting the server fixed the problem temporarily. The newly published photos would show up, but then any photos published after the server restart again failed to display.
The problem is that the
ShowPhotosView class is instantiated when the server starts.
ShowPhotosView.queryset gets set to the value returned by
Hero.objects.live(). That, in turn, is a
QuerySet, but it’s a
as_of set to
timezone.now() WHEN THE SERVER STARTS UP. That
as_of value never gets updated, so newer photos never get captured in the query.
There’s probably multiple ways to fix this, but an easy one is:
1 2 3
Now, instead of the queryset being instantiated at server start-up, it’s instantiated only when
ShowPhotosView.get_queryset() is called, which is when a request is made.