[英]save ImageField mongoengine
I have following class definition in mongoengine orm: 我在mongoengine orm中有以下类定义:
import mongoengine as me
class Description(me.Document):
user = me.ReferenceField(User, required=True)
name = me.StringField(required=True, max_length=50)
caption = me.StringField(required=True, max_length=80)
description = me.StringField(required=True, max_length=100)
image = me.ImageField()
in my post method of my tornado web requesthandler: 在我的龙卷风web requesthandler的post方法中:
from PIL import Image
def post(self, *args, **kwargs):
merchant = self._merchant
data = self._data
obj_data = {}
if merchant:
params = self.serialize() # I am getting params dict. NO Issues with this.
obj_data['name'] = params.get('title', None)
obj_data['description'] = params.get('description', None)
path = params.get('file_path', None)
image = Image.open(path)
print image # **
obj_data['image'] = image # this is also working fine.
obj_data['caption'] = params.get('caption', None)
obj_data['user'] = user
des = Description(**obj_data)
des.save()
print obj_data['image'] # **
print des.image # This is printing as <ImageGridFsProxy: None>
** print obj_data['image'] and print image are printing following: **打印obj_data ['image']和打印图像打印如下:
<PIL.PngImagePlugin.PngImageFile image mode=1 size=290x290 at 0x7F83AE0E91B8>
but 但
des.image still remains None.
des.image仍然没有。
Please suggest me what is wrong here. 请告诉我这里有什么问题。
Thanks in advance to all. 在此先感谢所有人。
You can not just put PIL objects into a field with obj.image = image
that way. 您不能将PIL对象放入具有
obj.image = image
的字段中。 You must do: 你必须这样做:
des = Description()
des.image.put(open(params.get('file_path', None)))
des.save()
In other words, ImageField
should be filled with file object after creating an instance by calling put
method. 换句话说,在通过调用
put
方法创建实例后,应该使用文件对象填充ImageField
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.