在以下代码中遇到属性错误:
from mongoengine import *
import datetime
from bson import ObjectId
class Doc_1(EmbeddedDocument):
criteria_id = ObjectIdField(default=ObjectId)
field_name = StringField()
field_value =DynamicField()
dominant = BooleanField()
class Doc_2(Document):
created_date = DateTimeField(default=datetime.datetime.now)
type = StringField(required=True)
state = StringField(required=True)
d1 = Doc_1()
d1.field_name = "state"
d1.field_value = [a, b, c, d, e]
d1.save()
d2 = Doc_2()
d2.type = "Demo"
d2.state = "c"
d2.save()
def field_match(Doc_1, Doc_2):
x = Doc_1.field_name
y = Doc_1.field_value
if Doc_2.x in y:
print("Yes")
field_match(Doc_1, Doc_2)
AttributeError:'Doc_2'对象没有属性'x'
如何在mongoengine的函数中正确使用变量?
答案 0 :(得分:0)
答案如此简单我有点尴尬......
该函数应该使用变量而不是'。'
def field_match(Doc_1, Doc_2):
x = Doc_1.field_name
y = Doc_1.field_value
if Doc_2[x] in y:
print("Yes")