Adding product images programmatically

67 views
Skip to first unread message

Evgeniy Ivchenkov

unread,
May 28, 2020, 8:38:12 AM5/28/20
to django-oscar
Hi oscar community!

I'm currently working on oscar-based ecommerce website and I need to create products programmatically based on parsed data.

Product creation is quite simple and I figured it out. But for the product images part it would be awesome to read some advices or recipies of how to do it right.

So it seems to me that if I simply download an img and store it im my MEDIA_ROOT and then add a database entity to catalogue_poroductimage providing product_id & path to original I'm missing out thumbnail generation and whatnot.

Looked through docs but didn't find details on the matter.

Can someone with experience on the subject share some knowledge?

Thanks!

Thorsten Mauch

unread,
May 30, 2020, 12:21:44 PM5/30/20
to django-oscar
I used the solution from here:




 
from oscar.apps.catalogue.models import ProductImage

 product_entry
= Product()
 product_entry
.title = my_product_title
 product_entry
.product_class = my_product_class
 product_entry
.category = my_category
 product_entry
.save()


'''
from here is relevant for the image
'''

 product_image
= ProductImage()
 image
= File(open("product_images/my_product_filename.jpg", 'rb'))
 product_image
.original = image
 product_image
.caption = "Some Caption"
 product_image
.product = product_entry
 product_image
.save()

it's important to copy the image files
MEDIA_ROOT. Ousite the import seem to be working, but later Oscar can't access the files

Hope this helps
Thorsten

Evgeniy Ivchenkov

unread,
Jun 1, 2020, 6:52:05 AM6/1/20
to django-oscar
Thanks a lot!

суббота, 30 мая 2020 г., 19:21:44 UTC+3 пользователь Thorsten Mauch написал:
Reply all
Reply to author
Forward
0 new messages