我得到一个'异常值:期望的字符串或缓冲区'每次我提交表格。看起来似乎问题出在模型领域' pub_date'。我附上了我的代码和我正在追溯的追溯。
我的问题是,这里到底出了什么问题,是否有办法解决这个问题?
#MODELS
class Entry(models.Model):
pub_date=models.DateTimeField(default=datetime.datetime.now,blank=True)
#FORMS
class SellForm(ModelForm):
class Meta:
model = Entry
fields = ['pub_date']
#VIEWS
def get_entry(request):
if request.method == 'POST':
f = SellForm(request.POST)
if f.is_valid():
obj=f.save(commit=False)
obj.save()
return HttpResponseRedirect('/storefront/')
else:
f = SellForm()
return render(request, 'sell.html', {'form': f})
Traceback:
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/benjamino/Desktop/deal/deal/accounts/views.py" in get_entry
27. form.save()
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/base.py" in save_base
573. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
654. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert
687. using=using, raw=raw)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
232. return insert_query(self.model, objs, fields, **kwargs)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1511. return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
897. for sql, params in self.as_sql():
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
855. for obj in self.query.objs
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
350. prepared=False)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
911. value = self.get_prep_value(value)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
895. value = self.to_python(value)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in to_python
854. parsed = parse_datetime(value)
File "/Users/benjamino/Desktop/deal/lib/python2.7/site-packages/django/utils/dateparse.py" in parse_datetime
67. match = datetime_re.match(value)
Exception Type: TypeError at /sell/
Exception Value: expected string or buffer
答案 0 :(得分:0)
我是因为这里:
obj=f.save(commit=False)
obj.save()
您正在保存整个表格。为了什么?
我认为从表单字段中提取数据并使用它们会更好。
有一个内置的表单cleaning_data属性。 f.cleaned_data
- 你会得到字典
形式字段值。