From f71f822dabe255fd0c3cbb26aedda462bf283399 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 16 Feb 2021 10:26:26 +0000 Subject: [PATCH] addtestdata correctly saving images for product --- core/management/commands/addtestdata.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/management/commands/addtestdata.py b/core/management/commands/addtestdata.py index ca88287..2d98449 100644 --- a/core/management/commands/addtestdata.py +++ b/core/management/commands/addtestdata.py @@ -60,8 +60,8 @@ class Command(BaseCommand): # create and assign products to companies for company in new_companies: - print("Creating fake products for {company.company_name}") - logging.info(f"Creating Products for {company.company_name}") + print(f"Creating fake products for {company.company_name}") + logging.info(f"Creating fake Products for {company.company_name}") # for i in range(100): for i in range(10): # make up data @@ -77,9 +77,11 @@ class Command(BaseCommand): response.raw.decode_content = True image = Image.open(response.raw) - # read image from memory + img_io = BytesIO() image.save(img_io, format='JPEG') + # option 1: read image from memory + ''' product.image = InMemoryUploadedFile( BytesIO(), field_name=None, @@ -88,10 +90,15 @@ class Command(BaseCommand): size=img_io.tell, charset=None ) + ''' + # option 2: File object + + product.image.save(f"{company.company_name}-{name}.jpg", File(img_io), save=False) product.save() logging.debug(f"New Product {product.name} created") print("*", end = '.') + # import ipdb; ipdb.set_trace() print('') print("Dataset creation finished") \ No newline at end of file