The function of uploading multiple pictures can be configured in the django admin background management system

Table of contents

 1. The default conventional method can only upload an example of a picture

2. Configuration can upload multiple pictures


Problem: In the admin background management system that comes with django, only one picture can be uploaded in a conventional way, and multiple pictures cannot be uploaded and added, as shown in the figure below. So now you need to configure the function of uploading multiple pictures!

 1. The default conventional method can only upload an example of a picture

models models.py

class Product(models.Model):
    """常规商品"""
    name = models.CharField(verbose_name="商品名称", max_length=50)
    desc = models.TextField(verbose_name="描述")
    show_img = models.ImageField(verbose_name="展示图", upload_to='images/')
    detail_img = models.ImageField(verbose_name="详情图", upload_to='images/')

    is_sale = models.BooleanField(verbose_name="是否上架", default=False)  # 为False时是下架
    is_show = models.BooleanField(verbose_name="是否展示在首页", default=True)  # 为True时是展示在首页
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="最后一次修改时间")


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "常规商品"
        verbose_name_plural = verbose_name

When the above Product model is configured in the admin background, only one display image show_img and detail image detail_img can be uploaded. The current requirement is that both display image show_img and detail image detail_img can upload multiple images!

2. Configuration can upload multiple pictures

1. Configure model models.py

class Product(models.Model):
    """常规商品"""
    name = models.CharField(verbose_name="商品名称", max_length=50)
    desc = models.TextField(verbose_name="描述")
    # show_img = models.ImageField(verbose_name="展示图", upload_to='images/')
    # detail_img = models.ImageField(verbose_name="详情图", upload_to='images/')

    is_sale = models.BooleanField(verbose_name="是否上架", default=False)  # 为False时是下架
    is_show = models.BooleanField(verbose_name="是否展示在首页", default=True)  # 为True时是展示在首页
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="最后一次修改时间")


    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "常规商品"
        verbose_name_plural = verbose_name


class ProductShowImage(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name="常规商品", related_name='imgs')
    show_img = models.ImageField(upload_to='show_images/', verbose_name="展示图")
    detail_img = models.ImageField(upload_to='detail_images/', verbose_name="详情图")

    def __str__(self):
        return ""

    class Meta:
        verbose_name = "常规商品图"
        verbose_name_plural = verbose_name

Comment out the display image show_img and detail image detail_img in the original model Product, add a ProductShowImage class, the model Product and ProductShowImage have a one-to-many relationship, and put the display image show_img and detail image detail_img in the table where the foreign key is located.

2. Configure the admin.py file

class ProductShowImageInline(admin.TabularInline):
    model = ProductShowImage
    can_delete = True
    extra = 3  # 额外展示一个添加图片选项(默认就是3)
    max_num = 6  # 最多添加6张图片


class ProductAdmin(admin.ModelAdmin):
    """常规商品"""
    inlines = [ProductShowImageInline]


admin.site.register(Product, ProductAdmin)  # 注册到admin后台

After the above configuration, you can display multiple pictures that can be uploaded in the admin background, as shown below

Guess you like

Origin blog.csdn.net/qq_37140721/article/details/130800591