我正在尝试将数据保存在表单中。在检查表单是否有效并尝试保存数据后,我收到了 AttributeError'list'对象没有属性'_committed'。我不明白问题是什么。这是我的表格。
class issueAddForm(forms.Form):
related_to = forms.ChoiceField(label = "Project")
title = forms.CharField(label = "Heading")
description = forms.CharField(label = "Description",widget=forms.Textarea )
attachments = forms.FileField(label = "Attachment(s)",widget=forms.ClearableFileInput(attrs={'multiple': True}))
def __init__(self, user, *args, **kwargs):
super(issueAddForm, self).__init__(*args, **kwargs)
self.fields['related_to'].choices = [(o.id, o.name) for o in Projects.objects.filter(client=user.id)]
views.py
def addIssue(request):
if request.method == "POST":
form= issueAddForm(request.user,request.POST,request.FILES)
if form.is_valid():
pr = Projects.objects.get(id=form.cleaned_data['related_to'])
ti = form.cleaned_data['title']
de = form.cleaned_data['description']
o = issues(related_to=pr,title=ti,description=de,attachments=request.FILES.getlist('attachments'))
o.save()
return HttpResponse("added")
跟踪:
Traceback (most recent call last):
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Capcee\Desktop\django\cap\support\views.py", line 67, in addIssue
o.save()
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\base.py", line 779, in save_base
force_update, using, update_fields,
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\base.py", line 908, in _do_insert
using=using, raw=raw)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1331, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1275, in as_sql
for obj in self.query.objs
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1275, in <listcomp>
for obj in self.query.objs
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1274, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\sql\compiler.py", line 1225, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\Users\Capcee\Desktop\django\env\lib\site-packages\django\db\models\fields\files.py", line 286, in pre_save
if file and not file._committed:
AttributeError: 'list' object has no attribute '_committed'