python - 什么可能导致无法将 `int` 存储到 `IntField` ?

标签 python mongoengine flask-mongoengine

我有以下 MongonEngine 模型:

from app import db
from datetime import datetime
from mongoengine import signals


class PathEmbedded(db.EmbeddedDocument):
    """
        To be embedded.
    """

    _id = db.ObjectIdField(required=False)
    distance = db.IntField(required=False, min_value=0, default=0)
    meta = {
        "allow_inheritance": True,
    }

    def __unicode__(self):
        return "Path '%s': %d m" % (self.id, self.distance)


class Path2(PathEmbedded, db.Document):
    """
        Same as above, but standalone version to be stored in its own collection.
    """

    _id = db.ObjectIdField()
    orig = db.ObjectIdField(required=True)
    dest = db.ObjectIdField(required=True)
    updateStamp = db.DateTimeField(required=True)
    ok_to_use = db.BooleanField(required=True, default=False)
    meta = {
        'indexes': [
            {
                'fields': ['ok_to_use', 'orig', 'dest'],
                'cls': False,       # does this affect performance?!
            },
        ],
    }

    @classmethod
    def pre_save(cls, sender, document, **kwargs):
        document.updateStamp = datetime.utcnow()

    def to_embedded(self):
        """
            Converts the standalone Path instance into an embeddadle PathEmbedded instance.
        """

        import json
        temp = json.loads(self.to_json())

        #remove the {"_cls": "Location"} key.
        #If we don't do this, the output will be a 'Location' instance, not a 'LocationEmbedded' instace
        temp.pop('_cls')

        return PathEmbedded().from_json(json.dumps(temp))



    def get_from_gmaps(self):
        """
            Get distance from Google maps using the directions API and append to the 'paths' list.
            Return False on error or True on success.
        """

        try:
            self.distance = 10,
            self.save()

        except Exception, e:
            print str(e)
            return False

        else:
            return True

# connect event hooks:
signals.pre_save.connect(Path2.pre_save, sender=Path2)

因此,在某些时候我会通过调用 get_from_gmaps() 来更新路径实例:

from app.models.Path2 import Path2 as P
from bson import ObjectId

p=P(orig=ObjectId(), dest=ObjectId())
p.save()
p.get_from_gmaps()

这会引发:

>>> p.get_from_gmaps()
ValidationError (Path2:54d34b97362499300a6ec3be) (10 could not be converted to int: ['distance'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "[...]app/models/Path2/get_from_gmaps.py", line 18, in get_from_gmaps
    self.save()
  File "[...]venv/local/lib/python2.7/site-packages/mongoengine/document.py", line 224, in save
    self.validate(clean=clean)
  File "[...]venv/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 323, in validate
    raise ValidationError(message, errors=errors)
ValidationError: ValidationError (Path2:54d34b97362499300a6ec3be) (10 could not be converted to int: ['distance'])

最初,我存储了一个从某些 json 解析并转换为 int 的整数,并认为那里出了问题,但我将其替换为 int 值以进行调试,现在得到了这个。我真的不知道从哪里开始o.O

编辑:扩展代码以提供完整的[非]工作示例。

最佳答案

10 后面多了一个逗号:

self.distance = 10,
                  ^

您正在将 distance 设置为包含 int 的元组,而不是 int。

<小时/>

提示:您看到如此无用消息的原因是 MongoEngine 不正确地使用 %s 格式字符串。事实上,"%s"% Something 的结果取决于 something 的类型,因为元组是特殊情况。比较:

>>> '%s' % 10
'10'
>>> '%s' % (10,)
'10'
>>> '%s' % (10, 11)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> '%s' % ((10,),)  # This is the correct way of formatting strings
'(10,)'              # when the type of the argument is unknown.

这当然是 MongoEngine 的问题,但是如果你想避免代码中出现同样的错误,请记住始终在 % 运算符右侧使用元组,或者更好地使用.format() 方法。

关于python - 什么可能导致无法将 `int` 存储到 `IntField` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325427/

相关文章:

python - 为什么我在 ipython 上收到 'Unhandled exception in event loop' 错误

Python mongoengine - 保存后检索_id

python - 过滤 mongoengine 查询

python - Flask+MongoEngine : Set MongoEngine Document Field as Unique in pre existing Document

flask - 访问 Flask 应用程序中 mongoengine 实例的属性

python - 连接到我的程序时的奇怪行为

python - 了解 sklearn.feature_extraction.text 的 CountVectorizer 类中的 _count_vocab 方法

python - Matplotlib:限制绘图宽度同时允许灵活的高度

flask - 使用 mongoengine 运行 uwsgi

Python/Flask/MongoEngine DateTimeField