1

我在 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()

在我的龙卷风网络请求处理程序的 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'] 和 print image 打印如下:

<PIL.PngImagePlugin.PngImageFile image mode=1 size=290x290 at 0x7F83AE0E91B8>

des.image 仍然没有。

请建议我这里有什么问题。

在此先感谢大家。

4

1 回答 1

3

你不能用obj.image = image这种方式将 PIL 对象放入一个字段中。你必须这样做:

des = Description()
des.image.put(open(params.get('file_path', None)))
des.save()

也就是说,调用方法ImageField创建实例后应该填充文件对象。put

于 2012-12-13T09:31:23.947 回答