Created
June 4, 2015 22:56
-
-
Save sjkingo/b836272f0757b20f8016 to your computer and use it in GitHub Desktop.
Add a product image programatically in Cartridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filename = 'image.jpg' | |
source_fp = open(filename, 'rb') | |
p = Product.objects.get(...) | |
# Create the ProductImage if it doesn't exist already and assign to Product | |
images = ProductImage.objects.filter(description=filename) | |
if len(images) == 0: | |
the_file = File(source_fp) | |
the_image = p.images.create( | |
product=p, | |
file=the_file, | |
description=filename) | |
media_path = the_image.file.path.strip(settings.MEDIA_ROOT) | |
p.image = media_path | |
p.save() | |
# Copy the image over to all the variations | |
variations = p.variations.all() | |
for variation in variations: | |
variation.image = the_image | |
variation.save() |
Hey,
Where are you importing the File from?
the_file = File(source_fp)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(obviously not a complete example..)