I'm trying to update my records using XML...so far the first part of the task is done....what I'm wondering is how to get my images onto the saved object (I'm using imagekit for the image handling BTW). My models look like this:
class Photo(ImageModel):
name = models.CharField(max_length=100)
original_image = models.ImageField(upload_to='photos')
num_views = models.PositiveIntegerField(editable=False, default=0)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class IKOptions:
spec_module = 'my_app.specs'
cache_dir = 'photos'
image_field = 'original_image'
save_count_as = 'num_views'
class Room(models.Model):
...
images = generic.GenericRelation('Photo', blank=True, null=True)
...
The XML that I'm using for this is as below:
<room>
<sq_ft>...</sq_ft>
<size>...</size>
<bedrooms>...</bedrooms>
<images>
<image>photos/IMG_3406.JPG</image>
<image>photos/IMG_3416.JPG</image>
<image>photos/IMG_3409.JPG</image>
</images>
</room>
My question is how to get the images for a given room when looping through the XML file and save them against that record.
UPDATE 1 I've tried this bit so far:
if room.getElementsByTagName('image'):
photo = ""
for v in room.getElementsByTagName('images'):
photo = v.childNodes[0].nodeValue
room_photo = Photo.objects.create(content_object = room,
object_id = room.id, original_image = photo)
This does save the photo (somewhat), but then the original_image
field is always blank, meaning that I'm doing something wrong in the above piece of code. Any ideas?