在表单向导结束时,我有一个对象的自定义保存:
def done(self, form_list, **kwargs):
(...)
start = datetime.strptime(self.request.session['event_start'], '%Y-%m-%d %H:%M:%S')
duration = datetime.strptime(self.request.session['duration'], '%H:%M:%S').time()
EventInstance.objects.create(
event=event_obj,
start=start,
duration=duration,
recurring='0F',
)
(...)
我已经为最后一步手动编写了一个字符串,试图找出问题所在。当我尝试运行最终脚本时,回溯会使用recurring
突出显示TypeError
字段并告诉我Python expected string or bytes-like object
。
这是我的模特:
class EventInstance(models.Model):
event = models.ForeignKey(Event)
start = models.DateTimeField()
duration = models.DateTimeField()
recurring = models.CharField(max_length=2)
event_obj
是我在此过程中创建的Event
对象。有人可以帮我理解这个错误吗?
回溯:
Traceback:
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\formtools\wizard\views.py" in dispatch
237. response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\Zeratul\Documents\otherlane\Otherlane\Events\views.py" in post
181. return self.render_done(form, **kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\formtools\wizard\views.py" in render_done
357. **kwargs)
File "C:\Users\Zeratul\Documents\otherlane\Otherlane\Events\views.py" in done
239. recurring='0F',
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\query.py" in create
348. obj.save(force_insert=True, using=self.db)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\base.py" in save
734. force_update=force_update, update_fields=update_fields)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\base.py" in save_base
762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\base.py" in _save_table
846. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\base.py" in _do_insert
885. using=using, raw=raw)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\query.py" in _insert
920. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
973. for sql, params in self.as_sql():
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
931. for obj in self.query.objs
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
931. for obj in self.query.objs
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
929. ) for f in fields
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
710. prepared=False)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
1482. value = self.get_prep_value(value)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1461. value = super(DateTimeField, self).get_prep_value(value)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1317. return self.to_python(value)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\db\models\fields\__init__.py" in to_python
1420. parsed = parse_datetime(value)
File "C:\Users\Zeratul\Envs\otherlane\lib\site-packages\django\utils\dateparse.py" in parse_datetime
93. match = datetime_re.match(value)
Exception Type: TypeError at /events/post_event/
Exception Value: expected string or bytes-like object
修改
仔细观察追溯,似乎是&#34;持续时间&#34;领域。也许是因为我试图将时间保存为日期时间对象?
▼ Local vars
Variable Value
__class__ <class 'django.db.models.fields.DateField'>
value datetime.time(0, 30)
self <django.db.models.fields.DateTimeField: duration>
答案 0 :(得分:3)
它在最后一行突出显示,但显而易见的是,通过跟踪堆栈,它最终会出现在django的dateparse.py
文件中,这表明它已经显示出来了。\ n \ n与datetime
未正确解析的问题相关。回溯只会突出显示一个完整的语句,但由于您的create
函数是一个多行语句,它会在recurring='0F'
行上标记。换句话说,它告诉您,您的陈述EventInstance.objects.create(......)
存在问题。
我认为您的duration
字段为DateTimeField
,但您的duration
变量是datetime.time
对象。您显然无法将time
对象提供给datetime
字段。
解决方案很简单,将duration
字段更改为TimeField
。
关于TimeField
的Django doc。