Django Multiple Image Upload in Admin: A Complete Guide
Yes, Django supports multiple image uploads in the admin interface. You can use the built-in 'InlineModelAdmin' or create a custom form to allow multiple images to be uploaded at once. Here is an example:
# models.py
class Product(models.Model):
name = models.CharField(max_length=100)
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField(upload_to='product_images/')
# admin.py
from django.contrib import admin
from django.forms import inlineformset_factory
from .models import Product, ProductImage
class ProductImageInline(admin.TabularInline):
model = ProductImage
extra = 3
class ProductAdmin(admin.ModelAdmin):
inlines = (ProductImageInline,)
admin.site.register(Product, ProductAdmin)
In this example, we have a 'Product' model and a 'ProductImage' model with a foreign key to 'Product'. We define an 'inlineformset_factory' to create a formset for the 'ProductImage' model, and use it in the 'ProductImageInline' class. We then add the 'ProductImageInline' to the 'inlines' attribute of the 'ProductAdmin' class to allow multiple images to be uploaded for each product in the admin interface.
原文地址: https://www.cveoy.top/t/topic/mVw4 著作权归作者所有。请勿转载和采集!