Django:代表经过时间的最佳字段类型是什么? [英] Django: What's the best field type to represent time elapsed?

查看:59
本文介绍了Django:代表经过时间的最佳字段类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要节省一些任务要花给用户的时间.

I'm need to save the time that some task took to the user to finish it.

例如,假设您必须回答考试中的几个问题.如果您从上午10:00开始并在上午11:00完成,那么我想存储一下您花费的时间.在这种情况下,将是1小时或60分钟,即3600秒.

For example, suppose you have to answer a couple of questions of an exam. If you started at 10:00 am and finish it at 11:00am I'd like to store the time it took you to finish it. In this case it'd be 1hr or 60 minutes, or 3600 seconds.

之后,我想以一种漂亮的方式来显示它.假设某件事花费了527秒,我想显示:8'47.所以我需要一些模板标签来完成这项工作.

After that i'd like to display that in a pretty way. Suppose something took 527 seconds, i'd like to show: 8'47. So i'd need some template tag to do the work.

我当然见过 TimeField ,但是它使用的是datetime.time,我认为这不是适应这种情况的严格方法.

Of course i've seen TimeField, but it's using datetime.time, and I don't think it's the rigth fit for this situation.

我还只使用了存储秒数的 IntegerField 进行了分析(该级别的细节还可以).但是也许你们知道更好的选择.

I've also analized using just an IntegerField storing seconds (that level of detail is Ok). But maybe you guys know a better option.

谢谢!

推荐答案

一个存储秒数的整数字段可能最适合存储持续时间(很容易将其插入到 timedelta 中).当然,如果您认为将来可能要存储一秒的分数,则可以考虑将其设置为浮点字段.

An integral field storing seconds is probably best for storing durations (it's easy to then plug that into a timedelta). Of course, if you think you might want to store fractions of a second in the future, you might want to consider making it a float field instead.

您还可以继承 IntegerField FloatField 的子类,并创建一个自动将内容与 timedelta 之间自动转换的版本.或使用某人已经创建的一个.

You could also subclass either IntegerField or FloatField and make a version that automatically translates the contents to and from a timedelta automatically... or use one that someone's created already.

这篇关于Django:代表经过时间的最佳字段类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆