I’ve had the good fortune of getting to learn and use Django for my job this past couple months. One of the really nice things about Django is the ORM and the API that allows me to do things such as this:
I’ve been working on the product database. One of the things that needed to be done was to take a bunch of products from the old site and put them on our site so that we can get a feel for what it will look like. Books like “Test book 1″ by “someone” only go so far.
One thing that is associated with each product is a thumbnail picture of the product and a bigger picture of the product. So, for the product ltpr-18758, there are two pictures:
ltpr-18758_t.jpg. Last night I had a directory full of pictures and an excel file full of product information. So I played with it a bit and was able to pull all of the information into the database. It was cool. To save time, I just randomly added two picture to each product. This morning, I think it would be better to have the pictures be correct, as well as to associate picture and products correctly.
Django has a spiffy API to access data.
This will get all of the products:
>>> product_list = Product.objects.all()
Then, I could do something like this to get the title for the 10th book:
'Some Random Book'
What I want to do is for all products, take their product code, add a “_t.jpg” or “_n.jpg” and set that to the image1 and image2 fields. Like so:
for product in product_list: product.image1 = "item-images/" + product.internal_code.lower() + "_t.jpg" product.image2 = "item-images/" + product.internal_code.lower() + "_n.jpg" product.save()
So, you’ll get an image1 like