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
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")