Загрузка нескольких изображений в админке Django

У меня есть категория, и для каждой категории я хочу добавить несколько изображений, но что бы я ни делал, у меня есть только один файл загрузки, но я хочу иметь несколько. В моем проекте Django есть такой класс:

в моем файле модели:

class SalonSampleImages(models.Model):
    service = models.ForeignKey(Salon, on_delete=models.CASCADE)
    Category = models.ForeignKey(
        ServiceCategory, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True, 
        verbose_name="the main service", 
        default=1
    )
    image = models.ImageField(upload_to='salon_images/', verbose_name="images")  

    def __str__(self):
        return f"images"

    class Meta:
        verbose_name = "picture"
        verbose_name_plural = "pictures"

в моем файле администратора:

class SalonSampleImagesInLineFormSet(forms.BaseInlineFormSet):
    def clean(self):
        super().clean()
        total_images = len([form for form in self.forms if form.cleaned_data and not form.cleaned_data.get('DELETE', False)])
        if total_images < 3:
            raise ValidationError("you must all at least 3 images")
        if total_images > 10:
            raise ValidationError("you cannot add more than 10 images")
        
class SalonSampleImagesInLine(admin.TabularInline):
    model = SalonSampleImages
    formset = SalonSampleImagesInLineFormSet
    extra = 1

и я зарегистрировал всех

Затем я создал файл модели:

class MultiFileInput(forms.ClearableFileInput):
    allow_multiple_selected = True

    def __init__(self, attrs=None):
        if attrs is None:
            attrs = {}
        attrs.setdefault('multiple', 'multiple')
        super().__init__(attrs=attrs)

    def value_from_datadict(self, data, files, name):
        return files.getlist(name)

class MultiImageUploadForm(forms.Form):
    category = forms.ModelChoiceField(
        queryset=ServiceCategory.objects.all(),
        label="choose category"
    )
    images = forms.FileField(
        widget=MultiFileInput(),  
        label="upload pictures"
    )

Затем я создал html-файл:

{% extends "admin/base_site.html" %}

{% block content %}
  <h1>{{ title }}</h1>
  <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="save the image" class="default">
  </form>
{% endblock %}

и мой settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

From my knowledge, Django admin interface doesn't support multiple files uploads out of the box, however, you can overcome this limitation by installing a third-party library for you to sort it
out - django-admin-multiupload via using pip to install it.
Update your admin.py file to use this code instead.

```py

from django.contrib import admin
from admin_multiupload.admin import MultiUploadAdmin
from <myapp>.models import SalonSampleImages

class SalonSampleImagesAdmin(MultiUploadAdmin):
    list_display = [
        'service',
        'category',
        'image'
    ]
    list_filter = [
        'service',
        'category'
    ]

    def process_upload_file(self, uploaded, instance, request):
        'Process the salon uploaded file'
        pass

admin.site.register(SalonSampleImages, SalonSampleImagesAdmin)


# For your forms code, you need to modify it to enable user upload 
 multiple files at a time with the usage of widget and FileField class

from django import forms
class MultiImageUploadForm(forms.Form):
    category = forms.ModelChoiceField(
        queryset=ServiceCategory.objects.all(),
        label='choose category you want'
    )
    images = forms.FileField(
        widget=forms.ClearableFileInput(attrs={'multiple': True}),
        label='upload pictures'
    )

NB: if you wanna apply the multiimageform class we created then you need to create a view function for that and you should use getList queryDict of request.FILES part of django. If you need any help from there, still let us know, for now let's focus on your admin interface question, which I had answered above.

Вернуться на верх