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
- [Answered ]-How to remove whitespaces in django urls?
- [Answered ]-Django jquery ajax image upload
- [Answered ]-Django 1.6 app tests.py to tests subdirectory
- [Answered ]-Django rest framework does not show utf8 words
Source:stackexchange.com