0

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

cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • What is it supposed to signify? 36.3 what? – Daniel Roseman May 11 '15 at 15:48
  • 2
    You probably don't want to use a `TimeField` to represent a number of seconds. `TimeField`s are used to store a specific time of day, not a `timedelta`. In Django 1.8, you'd use a `DurationField`. In previous versions, either store it as a number field with whatever level of precision you care about or use one of the third party field implementations - see this answer for a bunch of links: http://stackoverflow.com/questions/801912/how-to-put-timedelta-in-django-model – Peter DeGlopper May 11 '15 at 16:00
  • @PeterDeGlopper Could you make your response into an answer? – cybertextron May 11 '15 at 16:04

1 Answers1

1

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! :)

Gerard
  • 9,088
  • 8
  • 37
  • 52
  • 1
    If you can spare the precission loss a float by nature has, then yes. Otherwise, a field like `DecimalField` or the ones in the answer would be more recommended :) – Gerard May 11 '15 at 16:12