为商店中的产品上传多张照片

问题描述 投票:0回答:1

我正在使用 Django 加载商店网站。 我需要为一件产品上传 4 张照片,但我只能上传一张照片 你能帮我吗?

我的产品型号:

class Product(models.Model):
    Name = models.CharField(max_length=60)
    Descriptions = models.TextField()
    Price = models.IntegerField()
    Image = models.ImageField(upload_to='product')
    category = models.ManyToManyField(Category, null=True, blank=True)
    Discount = models.SmallIntegerField(null=True, blank=True)
    Size = models.ManyToManyField(ProductSize, related_name='product')
    Color = models.ManyToManyField(ProductColor, related_name='product', blank=True)
    Property = models.ManyToManyField(ProductProperty, related_name='product')

我对产品的看法:

class ProductDetail(DetailView):
    template_name = 'product/single-product.html'
    model = Product
python html django django-models django-views
1个回答
0
投票

选项 - 1:使用ForeignKey上传多张图片

class Images(models.Model):
    post = models.ForeignKey(Product, default=None)
    image = models.ImageField(upload_to=get_image_filename,
                              verbose_name='Image')

选项 - 2:无图像字段

这是首选方式 - 我已将其应用到我的项目中

1. Add image at a specific location (at filesystem or AWS S3)
2. Like: prod_images/product_id/image1.jpg, prod_images/product_id/image2.jpg ...
3. In your views you can respond back with above images links

© www.soinside.com 2019 - 2024. All rights reserved.