0

Running into an Attribute error on the code below:

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' object has no attribute 'x'

How do I properly use a variable within a function with mongoengine?

pb3975
  • 9
  • 1

1 Answers1

0

An answer so simple I'm a bit embarrassed...

The function should use brackets around the variable not '.'

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")
pb3975
  • 9
  • 1