extras-mongoengine IntEnum, constructor initialization failing

260 views
Skip to first unread message

Pratik Jain

unread,
Mar 4, 2016, 12:33:48 AM3/4/16
to MongoEngine Users
from enum import Enum
from extras_mongoengine.fields import IntEnumField
from mongoengine import Document


class IntEnum(Enum):
    FIRST = 1
    SECOND = 2

class Doc(Document):
    int_enum = IntEnumField(IntEnum)

def test():
    doc = Doc(int_enum = IntEnum.FIRST)
    doc.save()

The first line of test() leads to this error:
 File "/<base_path>/lib/python2.7/site-packages/mongoengine/fields.py", line 189, in to_python
    value = int(value)
TypeError: int() argument must be a string or a number, not 'IntEnum'

However,
def test():
    doc = Doc()
    doc.int_enum = IntEnum.FIRST
    doc.save()

This works well.

Shouldn't this be handled by IntEnum definition. Can anyone suggest a workaround(except turning this to StringEnum, which works)?

Message has been deleted

Chitrabasu Khare

unread,
Mar 16, 2016, 7:33:15 AM3/16/16
to MongoEngine Users
Hi,

I updated EnumField method in the extras_mongoengine\field.py as below:

    def to_python(self, value):
        return self.enum(super(EnumField, self).to_python(value.value))

It works correctly,can you please check once. You can make similar changes in fields.py in your local.
Reply all
Reply to author
Forward
0 new messages