我正在使用https://github.com/Chive/django-multiupload上的django-multiupload应用程序。
这似乎工作得很好,但我之前使用的是一个模型,并用一个带有默认multipupload字段的小部件替换了我之前的表单字段。
这似乎没有模型形式的表单域。 我的表格会以同样的方式运作吗?
似乎form.save()函数没有相应的行为,我得到这个错误:
Traceback: File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/agatorano/Code/metascape/metascape.org/source/uploads/views.py" in new_page 81. form.save(for_page=sess_) File "/Users/agatorano/Code/metascape/metascape.org/source/uploads/forms.py" in save 43. return super().save() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save 457. construct=False) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save_instance 103. instance.save() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save 590. force_update=force_update, update_fields=update_fields) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save_base 618. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _save_table 699. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert 732. using=using, raw=raw) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method 92. return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py" in _insert 921. return query.get_compiler(using=using).execute_sql(return_id) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql 919. for sql, params in self.as_sql(): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in as_sql 877. for obj in self.query.objs File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in <listcomp> 877. for obj in self.query.objs File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in <listcomp> 875. for f in fields File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/files.py" in pre_save 299. if file and not file._committed: Exception Type: AttributeError at /list/new Exception Value: 'list' object has no attribute '_committed'我知道这与多个文件在表单中保存的事实有关,但我不知道解决方案。
我的表格:
from django import forms from uploads.models import Document,Sess from multiupload.fields import MultiFileField from pandas_sql import * class DocumentForm(forms.models.ModelForm): #docfile = forms.FileField(widget=forms.FileInput(attrs={ # 'class':'filestyle','data-classButton':"btn btn-primary", # 'data-input':"false",'data-classIcon':"icon-plus", # 'data-buttonText':"Upload Data",})) docfile = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5) is_multiple = forms.BooleanField(label='Multi-Gene List',required=False,widget=forms.CheckboxInput()) class Meta: model=Document fields=('docfile','is_multiple',) def clean_docfile(self): docfile = self.cleaned_data.get('docfile',False) name = docfile.name extension = os.path.splitext(name)[1] if extension != '.xls' and extension != '.xlsx': raise forms.ValidationError("only excel files allowed ") if docfile._size<int("27000"): raise forms.ValidationError("File Not Large Enough For Upload") return docfile def save(self,for_page): self.instance.sess = for_page return super().save()这是我的模特:
from django.db import models from django.core.urlresolvers import reverse class Sess(models.Model): def get_absolute_url(self): return reverse('view_page',args=[self.id]) class Document(models.Model): docfile = models.FileField(upload_to='documents/%Y/%m/%d') is_multiple = models.BooleanField(default=False) sess = models.ForeignKey(Sess,default=None)评论部分是我之前的代码。 我是否需要将表单文件还原为标准表单文件或者模型仍然有效? 谢谢
I am using the django-multiupload app found at https://github.com/Chive/django-multiupload.
This appears to work well but I was using a modelform previously and replaced my prior form field with a widget with their default multipupload field.
This doesn't seem to have a formfield for modelforms. Would my form still function in the same way?
It seems that the form.save() function is not acting accordingly, I get this error:
Traceback: File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/agatorano/Code/metascape/metascape.org/source/uploads/views.py" in new_page 81. form.save(for_page=sess_) File "/Users/agatorano/Code/metascape/metascape.org/source/uploads/forms.py" in save 43. return super().save() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save 457. construct=False) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save_instance 103. instance.save() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save 590. force_update=force_update, update_fields=update_fields) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save_base 618. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _save_table 699. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert 732. using=using, raw=raw) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method 92. return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py" in _insert 921. return query.get_compiler(using=using).execute_sql(return_id) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql 919. for sql, params in self.as_sql(): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in as_sql 877. for obj in self.query.objs File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in <listcomp> 877. for obj in self.query.objs File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in <listcomp> 875. for f in fields File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/fields/files.py" in pre_save 299. if file and not file._committed: Exception Type: AttributeError at /list/new Exception Value: 'list' object has no attribute '_committed'I know that this has to do with the fact multiple files are being saved in the form, but I don't know a solution.
my form:
from django import forms from uploads.models import Document,Sess from multiupload.fields import MultiFileField from pandas_sql import * class DocumentForm(forms.models.ModelForm): #docfile = forms.FileField(widget=forms.FileInput(attrs={ # 'class':'filestyle','data-classButton':"btn btn-primary", # 'data-input':"false",'data-classIcon':"icon-plus", # 'data-buttonText':"Upload Data",})) docfile = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5) is_multiple = forms.BooleanField(label='Multi-Gene List',required=False,widget=forms.CheckboxInput()) class Meta: model=Document fields=('docfile','is_multiple',) def clean_docfile(self): docfile = self.cleaned_data.get('docfile',False) name = docfile.name extension = os.path.splitext(name)[1] if extension != '.xls' and extension != '.xlsx': raise forms.ValidationError("only excel files allowed ") if docfile._size<int("27000"): raise forms.ValidationError("File Not Large Enough For Upload") return docfile def save(self,for_page): self.instance.sess = for_page return super().save()and here is my model:
from django.db import models from django.core.urlresolvers import reverse class Sess(models.Model): def get_absolute_url(self): return reverse('view_page',args=[self.id]) class Document(models.Model): docfile = models.FileField(upload_to='documents/%Y/%m/%d') is_multiple = models.BooleanField(default=False) sess = models.ForeignKey(Sess,default=None)The commented part was my prior code. Do I need to revert my form file to a standard form file or does a modelform still work? Thank you
最满意答案
MultiFileField用于一次上传多个文件,而您的模型(您没有发布)显然只有一个文件字段。 有一个明显的例子,说明如何将MultiFileField与doc中具有单个文件字段的模型一起使用
The problem it turns out was that the form.save() function couldn't handle creating multiple model objects with the multiple.
I had to manually create each model object for each file in the list of files.
So I think modelForms were not appropriate in this instance.
更多推荐
发布评论