Hi,

I had to do something similar yesterday. I cloned a project which i'm
upgrading to Django 1.4 and the tip of satchmo, as well as other
enhancements.

The problem here, I have a lot of custom-models (not only products)
using the ImageWithThumbnailField, so I decided to keep them separatly
and modified the "upload_to" to save the somewhere else. But of course
I didn't want to upload and save everything manually again. So what I
did, I looped through all model-objects and imported the images from
the old-path. It worked.

This is the code to do what I did:

  1 from localsite.product.models import *
  2 from django.core.files import File
  3 import os
  4
  5 def change_bla_img_path():
  6     for s in Bla.objects.all():
  7         img = s.main_image  # this returns something analog to
ProductImage
  8         if not img:
  9             continue
 10         path = img.picture.path
 11         old_path = path.replace('/new_project/', '/old_project/')
 12         img.picture.save(
 13                 os.path.basename(old_path),  # extract the image-
name, like blabla.jpg
 14                 File(open(old_path))         # aparently needed to
save an ImageField. This will actually "import" the image to wherever
"upload_to" points to
 15                 )
 16         img.save()

In your case, you could calculate the path (i.e. sku_path) based on
the product's SKU, and maybe in a try-block open the img first, like f
= File(open(sku_path))... and then save it like
img.picture.save(os,path.basename(sku_path), f)

I hope that helps.

Regarding the other question:
"Also, if anyone have any ideas how I could have a variation display
images
the following way:

- If variation has an image, display it
- Otherwise, display image of the configurable image "

Well, I thought that should happen automatically??? ... I made some
tests in the past (we're not using variations yet), and usually the
variations return non-set-data from their parents (price, description,
etc.) ... I'm certain I saw this happen with price and description...
is it maybe a bug?


On Jun 19, 4:10 am, mel bin <[email protected]> wrote:
> We have about 700 product variations. We have all the images named based on
> the SKU and variation options of the products.
>
> I am trying to write a script to import images. However, I cannot create a
> ProductImage object for products:
>
> from satchmo_utils.thumbnail.field import ImageWithThumbnailField
> from product.models import Product, ProductImage
>
> >>> from satchmo_utils.thumbnail.field import ImageWithThumbnailField
> >>> from product.models import Product, ProductImage
> >>> p =
> KeyboardInterrupt
> >>> p = Product.objects.get(sku='2833')
> >>> f = open("/tmp/image_export/2833.jpg")
> >>> i = ImageWithThumbnailField(f)
> >>> pi = ProductImage(p.id, i)
> >>> pi.save()
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File
> "/var/www/virtualenv/satchmo_stage/lib/python2.6/site-packages/django/db/models/base.py",
> line 460, in save
>     self.save_base(using=using, force_insert=force_insert,
> force_update=force_update)
>   File
> "/var/www/virtualenv/satchmo_stage/lib/python2.6/site-packages/django/db/models/base.py",
> line 546, in save_base
>     for f in meta.local_fields]
>   File
> "/var/www/virtualenv/satchmo_stage/lib/python2.6/site-packages/django/db/models/fields/subclassing.py",
> line 28, in inner
>     return func(*args, **kwargs)
>   File
> "/var/www/virtualenv/satchmo_stage/lib/python2.6/site-packages/django/db/models/fields/related.py",
> line 872, in get_db_prep_save
>     if value == '' or value == None:
>   File
> "/var/www/virtualenv/satchmo_stage/lib/python2.6/site-packages/django/db/models/fields/__init__.py",
> line 124, in __cmp__
>     return cmp(self.creation_counter, other.creation_counter)
> AttributeError: 'str' object has no attribute 'creation_counter'
>
> Also, if anyone have any ideas how I could have a variation display images
> the following way:
>
> - If variation has an image, display it
> - Otherwise, display image of the configurable image
>
> Thanks for any help you may offer!

-- 
You received this message because you are subscribed to the Google Groups 
"Satchmo users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/satchmo-users?hl=en.

Reply via email to