When storing List of Enums in MongoEngine, enums aren't returned when reading the from the database

Viewed 819

When using a ListField of EnumField, the data in the objects returned from MongoEngne is not converted back to Enums. For example:

class DaysOfTheWeek(Enum):
   Mon = 1
   Tue = 2
   # etc ... 

class C(db.Document):
    days = db.ListField(db.EnumField(DaysOfTheWeek))

c1 = C(days=[DaysOfTheWeek.Mon])
assert c1.days ==  [DaysOfTheWeek.Mon]   # OK 
c1.save()
c2 = C.objects.first()
assert c2.days == [1]                    # OK 
assert c2.days == [DaysOfTheWeek.Mon]    # Fails

Is this expected behaviour? Am I using MongoEngine incorrectly?

I am using MongoEngine 0.22.1.

1 Answers

Just replace

assert c2.days == [DaysOfTheWeek.Mon] 

with:

assert c2.days == [DaysOfTheWeek.Mon.value]
Related