我的Django
型号上有以下字段:
ValidationError:[u"' 36.332' value的格式无效。它必须是HH:MM [:ss [.uuuuuu]]格式。"]
from django.db import models
class TestSuite(models.Model):
time = models.TimeField()
当我解析我的.xml
测试报告文件时,我有以下字段:
'time': u'36.332'
当我尝试通过**kwargs
创建模型时,我发现以下错误:
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 :(得分:1)
TimeField
字段不用于保存和/或表达一段时间,例如&#34; x秒&#34;,而是一天中的某个时间,例如说&#34; X事件发生在03:00:00&#34 ;;在这里,&#34; 03:00:00&#34;是TimeField
的值。即你可能将其视为datetime.datetime
对象的时间部分。
如果您需要存储的是金额时间,您可以使用任何其他符合您需求的内置字段(IntegerField,CharField等)并自行操作。
根据您需要的值,django-timedeltafield或DurationField也可能有用。虽然DurationField
目前仅在django 1.8中可用。
我希望这有帮助! :)