select multiple files in django with single filef

2019-04-15 15:39发布

问题:

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?

回答1:

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.



回答2:

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.