[Answered ]-Reevaluating a model-level query

2👍

Aha. The full version of the Manager method has a big difference from the one you originally posted, and it’s there that the trouble is.

def activeAnnouncements(self, expires=datetime.datetime.now()):

This is one of the biggest Python gotchas: default function parameters are evaluated when the function is defined, not when it is called. So the default value for expiry will be set to whenever the server process was first started. Read the effbot’s explanation of the problem. (Note it’s a Python problem, not anything to do with Django querysets.)

Instead, do this:

def activeAnnouncements(self, expires=None):
    if expires is None:
        expires = datetime.datetime.now()
    activeAnnouncements = self.filter(expires_at__gt=expires).all()
    return activeAnnouncements

0👍

Is this an answer to your question?

Leave a comment