2015-05-19 27 views
1

我正在使用在https://github.com/Chive/django-multiupload找到的Django的多上傳應用程序。Django中的模型表單是否需要表單具有forms.fieldtype格式? form.save()不適用於multipleupload

這似乎工作得很好,但我之前使用了一個模型表單,並用默認的multipupload字段替換了我之前的表單字段。

這似乎沒有模型的formfield。我的表單仍然以同樣的方式運行?

看來,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) 

註釋的部分是我以前的代碼。我是否需要將表單文件還原爲標準表單文件或模型表單是否仍然有效?謝謝

+0

您對模型的使用對我來說似乎有點奇怪。而不是在其中清晰地顯示模型字段中的'docfile'和'is_multiple',我只會更改他們的小部件或任何必需的屬性。看到https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#overriding-the-default-fields並告訴我它是怎麼回事。 – Wtower

回答

0

它的結果問題是form.save()函數無法處理創建多個模型對象。

我必須爲文件列表中的每個文件手動創建每個模型對象。

所以我認爲在這種情況下modelForms不合適。

0

MultiFileField是一次上傳多個文件,你的模型(你沒有發佈)顯然有一個文件字段。有一個明確的例子,如何使用MultiFileField與一個單一的文件字段模型in the doc

+0

我很抱歉沒有發佈我的模型! 我沒有問題與我的模型匹配的形式,而是而不是我使用formmodel而不是標準形式的事實是適當的 – user1340048

+0

我特別是遇到form.save()函數的問題。 – user1340048

相關問題