I'm trying to write a Django query for widgets that are more than 5 hours old and I'm a bit lost. The widget model has a DateTimeField
that is populated with the creation time of the widget.
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- TypeError: 'BaseQuery' object is not calla
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
The simplest approach has already been covered by other answers: just filter the records where the date is earlier than five hours ago. Here's a full example that finds records created at least five seconds ago:
That shows me all but the last widget:
That works fine, unless you have enabled time zone support. In the previous example, I do that by changing
settings.configure(...
to look like this:When I do that, I get a message like this:
To get a time-zone-aware date, use
timezone.now()
function instead ofdatetime.now()
:Occasionally, I have had problems where the database's clock gets out of synch with the web server's clock. To avoid that kind of problem, you can use the
Now()
function to get the current time in the database.I haven't seen that problem in recent years, so it's probably not worth the hassle in most cases.
if settings.USE_TZ = True and settings.TIME_ZONE is setting
If
Widget
is the name of your model, and it has a DateTimeField attribute namedcreated
, the query would be:Note that
created__lt
means "created is less than".That should do the trick.