EmbeddedDocument中的MongoEngine ListField在验证时抛出TypeError

时间:2012-09-12 16:10:57

标签: python mongodb typeerror mongoengine

我不确定它是MongoEngine中的错误还是我错过了什么。 我设置了以下模型:

class Features(EmbeddedDocument):
    version = FloatField()
    data = ListField(StringField)

class Article(Document):
    vendor = ReferenceField(Vendor)
    url = URLField()
    author = StringField()
    clean_content = StringField()
    features = EmbeddedDocumentField(Features)

当我像这样测试我的模型时:

#add vendor
vendor = Vendor(name="techcrunch", config="vendor config")
vendor.save()

#create features
features = Features(version = 1.0)
features.data = ["5", "89"]

#add article
article = Article(vendor = vendor, url ="http://www.techcrunch.com", 
                  author ="MG Siegler", clean_content = "Apple rocks!")
article.features = features
article.save()

我收到以下错误:

TypeError: unbound method _validate() must be called with StringField instance as first argument (got str instance instead)

有人可以解释一下吗?

编辑:

没关系。我发现了我的错误。

必须是:

class Features(EmbeddedDocument):
    version = FloatField()
    data = ListField(StringField())

1 个答案:

答案 0 :(得分:14)

我发现了错误。

必须是:

class Features(EmbeddedDocument):
    version = FloatField()
    data = ListField(StringField())