Search code examples
pythongoogle-app-enginegoogle-cloud-datastoreapp-engine-ndbgoogle-app-engine-python

What are Best Practice for msgprop.EnumProperty and messages.Enum with Python 3 and Cloud NDB?


With Python 2 and NDB, we could store Enum in entities' properties this way:

from google.appengine.ext import ndb
from google.appengine.ext.ndb import msgprop
from protorpc import messages

class CoreWebhookService(messages.Enum):
    UNKNOWN = 0
    AUTH0 = 1

class CoreWebhook(ndb.model):
    service = msgprop.EnumProperty(CoreWebhookService, required=True)
    url = ndb.StringProperty(required=True)

With Python 3, based on this guide, it says If you try to create these objects, a NotImplementedError will be raised. for google.appengine.ext.ndb.msgprop.EnumProperty.

I understand

from google.appengine.ext import ndb

is getting replaced by

from google.cloud import ndb

but what's the best practice for Enum from now on since it not implemented?

Thank you


Solution

  • Sorry I don't have a test project to verify this, but python 3 has enums. You should be able to do something straight forward like:

    from enum import IntEnum
    
    class CoreWebhookService(IntEnum):
        UNKNOWN = 0
        AUTH0 = 1
    
    class CoreWebhook(ndb.model):
      service = IntegerProperty(required = true, choices=list(CoreWebhookService))
      url = ndb.StringProperty(required=True)