B
B
blazer052017-08-17 13:37:03
Django
blazer05, 2017-08-17 13:37:03

How to make additional images for a product?

Hello.
In the product model, I made a field for loading an image, but by default one image is loaded for one product, how can I make it so that there are 5-6 images for one product? I tried to make another model in which there will be a fk link to the product model and in which there will be additional pictures to the main product, but the problem is the same, only one picture can be uploaded.

class Product(models. Model):
    image = models.ImageField(upload_to='products/%y/%m/%d/', blank=True, verbose_name='Изображение товара')

class Albom(models.Model):
    name = models.CharField(max_length=100, blank=True, verbose_name='Название')
    product = models.ForeignKey('Product', on_delete=models.CASCADE, related_name='images', null=True)
    images = models.ImageField(upload_to='products/%y/%m/%d/', blank=True, verbose_name='Изображение')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Astrohas, 2017-08-17
@blazer05

blazer05 : in the admin is done via Inline( https://docs.djangoproject.com/en/1.11/ref/contrib... In forms via inline_formset( https://docs.djangoproject.com/en/1.11/topics/form ...

A
Alexey Sergeev, 2017-08-17
@SergeevAI

I did like this.
models.py

def images_upload_folder(instance, filename):
    return '{model}s/{filename}'.format(model=instance._meta.model_name, filename=filename)

class BannerImage(TimeStampedModel):
    banner = models.ForeignKey(Banner, related_name='images')
    image = models.ImageField(upload_to=images_upload_folder,  blank=True)
    sort = models.IntegerField(default=1000, verbose_name='Сортировка')
    is_active = models.BooleanField(default=True, verbose_name='Включить')

    objects = models.Manager()
    active = IsActiveManager()

    def __str__(self):
        return "{banner} > {file}".format(banner=self.banner.title, file=self.image.file)

admin.py
class BannerImageInLine(admin.TabularInline):
  model = BannerImage

class BannerAdmin(admin.ModelAdmin):
  inlines = [
      BannerImageInLine
  ]


admin.site.register(Banner, BannerAdmin)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question