保存 XML 节点中的通用图像
我正在尝试使用 XML 更新我的记录...到目前为止,任务的第一部分已完成...我想知道如何将我的图像保存到保存的对象上(我正在使用 imagekit图像处理顺便说一句)。我的模型如下所示:
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)
...
我为此使用的 XML 如下:
<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>
我的问题是如何在循环 XML 文件时获取给定房间的图像并将其保存到该记录中。
更新1 到目前为止我已经尝试过这一点:
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)
这确实保存了照片(在某种程度上),但是 original_image
字段始终为空,这意味着我在上面的代码中做错了一些事情。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看过 xml_models 吗?不确定它是否完全适合您,因为我不能 100% 确定您的要求。然而,它确实非常顺利地处理与使用 XML 的模型的关系,因此可能会顺便解决您的问题:-)
我认为 xml_models 会为您工作。让我知道!
Have you taken a look at xml_models? Not sure if it's exactly right for you because I'm not 100% sure of what you're asking. However, it does take care of relationships with models that use XML very smoothly, so may solve your problem incidentally :-)
I think xml_models will work for you. Let me know!
最终找到了解决方案:
认为这可能会帮助以后遇到类似问题的人。
Figured this out as the solution finally:
Thought this might help someone with a similar problem later on.