Reputation: 10961
I have the following field on my Django
model:
ValidationError: [u"'36.332' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]
from django.db import models
class TestSuite(models.Model):
time = models.TimeField()
when I parse my .xml
test report files, I have the following field:
'time': u'36.332'
When I try to create the model via a **kwargs
I'm seeing the following error:
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."]
How can I convert my string 'time'
to a TimeField()
object ?
* Note: *
'36.332'
means 36.332 seconds
Upvotes: 0
Views: 1599
Reputation: 9408
The TimeField
field is not used to save and/or express an amount of time, like "x seconds", but a time in the day, like saying "X event occurred at 03:00:00"; here, "03:00:00" is the value for TimeField
. Namely you might see it as the time part of a datetime.datetime
object.
If what you need to store is an amount of time, you can use any other built-in field (IntegerField, CharField, etc) that fits your needs and manipulate it yourself.
Depending on what you need the value for, django-timedeltafield or DurationField may be useful as well. Although DurationField
is only available in django 1.8 at the moment.
I hope this helps! :)
Upvotes: 1