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?
I believe this is caused by your view defining
queryset = Item.objects.published()as a class variable. This line will be executed once, when yourItemArchiveclass is initially imported. You should move that line into a method where it will be executed each time a view is called.