2015-05-11 60 views
0

我有以下領域對我Django型號:轉換時間字符串TimeField

ValidationError:[U「'36 0.332的價值有一個無效的格式必須是HH:MM [:SS [.uuuuuu] ]格式「]

from django.db import models 
class TestSuite(models.Model): 

    time = models.TimeField() 

當我分析我的.xml測試報告文件,我有以下領域:

'time': u'36.332' 

當我嘗試通過創建模型3210我看到了以下錯誤:

kwargs = {'time': u'36.332'} 
    testsuite = TestSuite(**kwargs) 

    Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 710, in save 
    force_update=force_update, update_fields=update_fields) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 738, in save_base 
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 822, in _save_table 
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 861, in _do_insert 
    using=using, raw=raw) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 127, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 920, in _insert 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 970, in execute_sql 
    for sql, params in self.as_sql(): 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 928, in as_sql 
    for obj in self.query.objs 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save 
    prepared=False) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 2293, in get_db_prep_value 
    value = self.get_prep_value(value) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 2288, in get_prep_value 
    return self.to_python(value) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 2275, in to_python 
    params={'value': value}, 
ValidationError: [u"'36.332' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."] 

如何將我的字符串轉換'time'TimeField()對象? *注:* '36.332'意味着36.332 seconds

+0

它應該表示什麼? 36.3什麼? –

+0

@DanielRoseman更新了問題 – cybertextron

+2

您可能不希望使用'TimeField'來表示秒數。 'TimeField'用於存儲一天的特定時間,而不是'timedelta'。在Django 1.8中,你會使用'DurationField'。在以前的版本中,將它作爲一個數字字段存儲,無論您關心的是什麼級別的精度,或者使用第三方字段實現之一 - 請參閱一些鏈接的答案:http://stackoverflow.com/questions/801912/ how-to-put-time-delta-in-django-model –

回答

1

TimeField字段不用於保存和/或表達的時間量,如「x秒」,但在一天的時間,就像說「X事件發生在03:00:00「;在這裏,「03:00:00」是TimeField的值。即你可能看到它作爲datetime.datetime對象的時間部分。

如果你需要存儲的是一個的數量的時間,你可以使用任何其他內置的字段(IntegerField,CharField等),以滿足您的需求並自己操作。

根據你所需要的值,django-timedeltafieldDurationField也可能有用。雖然DurationField目前僅在django 1.8中可用。

我希望這有助於! :)

+0

在這種情況下,「FloatField」會一樣好,正確嗎? – cybertextron

+1

如果你能夠避免精確度損失,那麼天生浮動就是了。否則,像'DecimalField'這樣的字段或者答案中的字段會更加推薦:) – Gerard