Ask Your Question
0

How can I add several files at once in Django admin?

asked 2021-12-19 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-04 05:00:00 +0000

ladyg gravatar image

In Django admin, you can add several files at once by using the Django Multiuploader app. To use this app, follow the steps below:

  1. Install the Django Multiuploader app using pip:
pip install django-multiuploader
  1. Add 'multiuploader' to your INSTALLED_APPS in settings.py and run the migrations:
INSTALLED_APPS = [
    # other apps
    'multiuploader',
]

python manage.py makemigrations
python manage.py migrate
  1. Create a new model for the files you want to upload:
from django.db import models
from multiuploader.models import MultiuploaderImage

class MyFile(models.Model):
    description = models.CharField(max_length=100)
    files = models.ManyToManyField(MultiuploaderImage, blank=True)

    def __str__(self):
        return self.description
  1. Create a new admin form for your model:
from django import forms
from multiuploader.forms import MultiuploaderFileForm
from .models import MyFile


class MultiImageForm(forms.ModelForm):
    class Meta:
        model = MyFile
        fields = '__all__'
        widgets = {
            'files': MultiuploaderFileForm()
        }
  1. Register your model and form in admin.py:
from django.contrib import admin
from .models import MyFile
from .forms import MultiImageForm


class MyFileAdmin(admin.ModelAdmin):
    form = MultiImageForm

admin.site.register(MyFile, MyFileAdmin)

With the above configuration, you can now upload multiple files at once in Django admin.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-19 11:00:00 +0000

Seen: 7 times

Last updated: Mar 04 '22