diff --git a/core/management/commands/addtestdata.py b/core/management/commands/addtestdata.py index 35d7bf9..3b91f22 100644 --- a/core/management/commands/addtestdata.py +++ b/core/management/commands/addtestdata.py @@ -35,11 +35,11 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): print("Create fake data to populate database\n") - + ''' print("Deleting existing Product and Company instances") Product.objects.all().delete() Company.objects.all().delete() - + ''' # start faker fake = Faker() Faker.seed(0) diff --git a/products/models.py b/products/models.py index 7ee89fd..196cc8c 100644 --- a/products/models.py +++ b/products/models.py @@ -34,6 +34,12 @@ class AttributeTag(TagTreeModel): # autocomplete_view = 'myapp.views.hobbies_autocomplete' +def product_image_path(instance, filename): + """Add the instance's id to the path + """ + return f"products/{instance.id}/{filename}" + + class Product(models.Model): SYNCHRONIZED = 'SYNCHRONIZED' @@ -50,7 +56,7 @@ class Product(models.Model): sku = models.CharField('Referencia de producto', max_length=255, null=True, blank=True ) name = models.CharField('Nombre del producto', max_length=500, null=True, blank=True) description = models.TextField('Descripción', null=True, blank=True) - image = models.ImageField('Imagen del producto', upload_to='products/', null=True, blank=True) + image = models.ImageField('Imagen del producto', upload_to=product_image_path, null=True, blank=True) url = models.URLField('URL del producto', null=True, blank=True, max_length=1000) price = models.DecimalField('Precio', max_digits=10, decimal_places=2, null=True, blank=True) shipping_cost = models.DecimalField('Gastos de envío', max_digits=10, decimal_places=2, null=True, blank=True)