addtestdata correctly saving images for product

This commit is contained in:
Sam
2021-02-16 10:26:26 +00:00
parent e2b6f02b37
commit f71f822dab

View File

@@ -60,8 +60,8 @@ class Command(BaseCommand):
# create and assign products to companies # create and assign products to companies
for company in new_companies: for company in new_companies:
print("Creating fake products for {company.company_name}") print(f"Creating fake products for {company.company_name}")
logging.info(f"Creating Products for {company.company_name}") logging.info(f"Creating fake Products for {company.company_name}")
# for i in range(100): # for i in range(100):
for i in range(10): for i in range(10):
# make up data # make up data
@@ -77,9 +77,11 @@ class Command(BaseCommand):
response.raw.decode_content = True response.raw.decode_content = True
image = Image.open(response.raw) image = Image.open(response.raw)
# read image from memory
img_io = BytesIO() img_io = BytesIO()
image.save(img_io, format='JPEG') image.save(img_io, format='JPEG')
# option 1: read image from memory
'''
product.image = InMemoryUploadedFile( product.image = InMemoryUploadedFile(
BytesIO(), BytesIO(),
field_name=None, field_name=None,
@@ -88,10 +90,15 @@ class Command(BaseCommand):
size=img_io.tell, size=img_io.tell,
charset=None charset=None
) )
'''
# option 2: File object
product.image.save(f"{company.company_name}-{name}.jpg", File(img_io), save=False)
product.save() product.save()
logging.debug(f"New Product {product.name} created") logging.debug(f"New Product {product.name} created")
print("*", end = '.') print("*", end = '.')
# import ipdb; ipdb.set_trace()
print('') print('')
print("Dataset creation finished") print("Dataset creation finished")