select multiple files in django with single filef

2019-04-15 15:34发布

How can I select multiple files in a single fileField in django? I want to select different file formats in a single filefiled in django admin.

**models.py**
 class FileModel(models.Model):
    name = models.CharField(max_length=100)
    file_upload = models.FileField(upload_to=settings=PATH)

**admin.py**
 admin.site.register(FileModel,FileAdmin);

In the admin I want to customize the file field to select multiple files to store in given path? Could you please help me?

2条回答
萌系小妹纸
2楼-- · 2019-04-15 15:53

You can't? The FileField was not designed for that purpose.

You would have to define your own field (possibly extended from FileField) that pickles the internal data (paths to multiple files) and stores them in the database and unpickles them back.

Of course, I don't see a reason to do this at all, when you can simply define one to many relationship or even many to many relationship between a new filelist model and existing filemodel. Then you could make an inline admin handler attached to the uploader model and you can add multiple files easily that way.

See inline admin models for details.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-04-15 16:12

I know this is old, but it comes up at the top of Google search so I thought I'd comment. You can use django-multiupload to do this.

# forms.py
from django import forms
from multiupload.fields import MultiFileField

class UploadForm(forms.Form):
     attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5)

Hope that helps.

查看更多
登录 后发表回答