使用日期时间字段模型django筛选MYSQL pull



我一直很难找到一个特定于从mysql数据库中过滤模型的解决方案。

模型文件:

class PreviousLossesMlbV5WithDateAgg(models.Model):
game_points_with_formula_field = models.FloatField(db_column='GAME POINTS WITH FORMULA:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
date_field = models.DateTimeField(db_column='DATE:', blank=True, null=True)  # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
class Meta:
managed = False
db_table = 'previous_losses_mlb_v5_with_date_agg'

查看文件

def indexsss(request):
monday_new = PreviousLossesMlbV5WithDateAgg.objects.values_list('game_points_with_formula_field',flat=True).filter(date_field= '2021-07-05')
monday_list = list(monday_new)

return render(request, "show4.html", context={'test45': monday_list}
)

数据不填充,也显示在终端中

DateTimeField PreviousLossesMlbV5WithDateAgg.date_field received a naive datetime (2021-07-05 00:00:00) while time zone support is active.

试试这个方法:

PreviousLossesMlbV5WithDateAgg.objects.filter(date_field__date= datetime.date(2021,07,05)).values_list('game_points_with_formula_field',flat=True)

我能够解决这个问题,因为在设置页面中指定了时区,我遇到了问题。对于我的应用程序,我只提取保存在数据库中的日期,所以我真的不需要Django来调整时区或类似的东西。我评论了

#TIME_ZONE = 'US/Mountain'
#USE_TZ = True