Django:DateTimeField收到了一个天真的日期时间

时间:2015-10-28 03:32:28

标签: python django django-views python-datetime django-timezone

我正在制作一个视图,该视图仅显示截止日期尚未过去的帖子。完成这个我添加了以下我的views.py文件:

current_posts = Posts.objects.filter(post_name=post, deadline__range=[date.today(), "2999-12-31"]).order_by('deadline')

这样我就可以只显示截止日期介于今天和2999年12月31日之间的帖子。但是我收到以下错误,并且根本没有帖子显示:

RuntimeWarning: DateTimeField OfferedFunds.deadline received a naive datetime (2999-12-31 00:00:00) while time zone support is active.
  RuntimeWarning)

过去发布的解决方案让我尝试了以下内容,但也无效:

timezone.now()
current_posts = Posts.objects.filter(post_name=post, deadline__range=[timezone.now(), "2999-12-31"]).order_by('deadline')

如果不是输入:timezone.now(),我会输入一个像“2015-10-10”这样的实际日期,但是这会失败。关于如何解决这个问题的任何想法?

3 个答案:

答案 0 :(得分:3)

您可以执行deadline__gte=timezone.now()

,而不是检查范围

此页面包含更多信息:https://docs.djangoproject.com/en/stable/ref/models/querysets/#gte

答案 1 :(得分:0)

我今天在工作中遇到了这个问题!基本上deadline__range=[date.today(), '2999-10-10']行在第一个时隙中具有UTC感知时间,在第二个时隙中具有UTC未感知时间。让我引导您访问帮助我How to make an unaware datetime timezone aware in python的帖子。有一些纯粹的python解决方案和一些django解决方案,但我想你会在那里找到你需要的东西!

答案 2 :(得分:0)

在您的 public function allProducts() { //call recursive method to append all children categories return $this->getAll([$this]) //select category products as each item ->pluck('products') //flatten products into one dimension array ->flatten() //remove repeated products ->unique(); } public function getAll($categories) { //appending categories array $append = collect(); //walk-through input categories foreach ($categories as $category) { //if category has children add them to $append if ($category->childs()->count()) { $append->merge($category->childs); } } //if there were children categories, also add their children to $append if ($append->count()) { $append = $this->getAll($append); } //merge children and categories return $categories->merge($append); } 中设置settings.py,这样可以解决警告。就我而言,我想在全球范围内使用本地日期时间,因此我最终要做的是。干杯!

相关问题