I have a model that looks like this:
class Item(models.Model):
...
publish_date = models.DateTimeField(default=datetime.datetime.now)
...
And a manager that looks like this:
from datetime import datetime
class ItemManager(Manager):
def published(self):
return self.get_query_set().filter(publish_date__lte=datetime.now()
And a view that looks like this:
class ItemArchive(ArchiveIndexView):
queryset = Item.objects.published()
date_field = 'publish_date'
The idea being that I can call Item.objects.published()
and get a queryset of all published Items
.
The problem is that Django seems to be executing the datetime.now()
call in the manager when the server is started and then caching that value. So if today is May 24th, and I created an Item
with a publish date of May 23rd, and started the server on May 22nd, that May 23rd item will not show up in the ItemArchive
view. As soon as I restart Apache, the May 23rd item shows up in the view correctly.
How can I force Django to execute datetime.now()
every time the manager is called?
First, just use
models.DateTimeField(auto_now_add=True)
Edited:
Second, use
get_queryset
method:Don't use the
queryset
class variable, and overrideget_queryset
instead. In general, I thinkqueryset
is a bit of a red herring. If you just specifymodel
instead, Django automatically sets the queryset toself.model.objects.all()
. If you need any filtering, 99 times out of 100 you'll need to overrideget_queryset
to be thread-safe. Try the following instead.I believe this is caused by your view defining
queryset = Item.objects.published()
as a class variable. This line will be executed once, when yourItemArchive
class is initially imported. You should move that line into a method where it will be executed each time a view is called.