Answer the question
In order to leave comments, you need to log in
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
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 ...
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)
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 questionAsk a Question
731 491 924 answers to any question