我使用的模型具有名为 start_time 和 finish_time 的字段,两者都是 DateTimeField,我试图通过用 start_time 减去 finish_time 来计算响应时间。在外壳中:>>> obj = Quizgame.objects.get(user=harry)>>> start = obj.start_time>>> end = obj.finish_time>>> result = end - start>>> resultdatetime.timedelta(seconds=31, microseconds=912224)但是在 django Views 中,当我执行此操作时,我会收到错误消息,即 -: 'method' 和 'datetime.datetime' 不受支持的操作数类型views.pydef final_part(request): obj = Quizgame.objects.get(user=request.user) start = obj.start_time end = obj.end_time result = end - start return HttpResponse(result)models.pyclass Quizgame(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) total_score = models.DecimalField("Total score", default=0, decimal_places=2, max_digits=6) start_time = models.DateTimeField(auto_now_add=True) finish_time = models.DateTimeField(null=True)我稍后使用 datetime.datetime.now() 设置完成时间
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
您使用 obj.end_time 而不是 obj.finish_time
def final_part(request):
obj = Quizgame.objects.get(user=request.user)
start = obj.start_time
end = obj.finish_time
result = end - start
return HttpResponse(result)
添加回答
举报
0/150
提交
取消