python - ValidationError in Django Model -
i defined test
model as:
class test(models.model): name = models.binaryfield(blank=false) time = models.timefield() # 1 test case has many tests testcase = models.foreignkey(testcase) def __str__(self): return self.name
when try parse xml
file create model object have following method:
def add_test(testcase, obj): kwargs = { 'name': "", 'time': none, } status = 1 # create test object k, v in obj.iteritems(): if k == '@name': kwargs['name'] = v if v not none else "" elif k == '@time': kwargs['time'] = v print kwargs test = test(testcase=testcase, **kwargs) test.save()
there, testcase
oject of testcase
model. kwargs
have correct input:
{'name': u'runtest', 'time': u'36.332'}
however, when invoke above function get:
traceback (most recent call last): file "dbsync.py", line 131, in <module> add_testsuite("scale") file "dbsync.py", line 55, in add_testsuite add_testcases(testsuite, testcases) file "dbsync.py", line 87, in add_testcases status = add_test(testcase_obj, testcase) file "dbsync.py", line 103, in add_test test.save() 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 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 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}, django.core.exceptions.validationerror
i don't quite what's going on there ... clues?
to make work have convert string datetime.time.
kwargs['time'] = datetime.datetime.strptime(v, 'your_time_format').time()
replace your_time_format input time format have.
Comments
Post a Comment