简体   繁体   中英

How to “POST” ndb.StructuredProperty?

Problem:

I have following EndpointsModels ,

class Role(EndpointsModel):
    label = ndb.StringProperty()
    level = ndb.IntegerProperty()

class Application(EndpointsModel):
    created = ndb.DateTimeProperty(auto_now_add=True)
    name = ndb.StringProperty()
    roles = ndb.StructuredProperty(Role, repeated=True)

and an API method:

class ApplicationApi(protorpc.remote.Service):
    @Application.method(http_method="POST",
                        request_fields=('name', 'roles'),
                        name="create",
                        path="applications")
    def ApplicationAdd(self, instance):
        return instance

When I try to POST this data:

{ "name": "test", "roles": [{ "label": "test", "level": 0 }] }

I'm getting an error ( trace ):

AttributeError: 'Role' object has no attribute '_Message__decoded_fields'

Workaround:

I tried to use EndpointsAliasProperty :

class ApplicationApi(protorpc.remote.Service):
    ...
    def roless_set(self, value):
        logging.info(value)
        self.roles = DEFAULT_ROLES

    @EndpointsAliasProperty(setter=roless_set)
    def roless(self):
        return getattr(self, 'roles', [])

which results in 400 BadRequest

Error parsing ProtoRPC request (Unable to parse request content: Expected type <type 'unicode'> for field roless, found {u'level': 0, u'label': u'test'} (type <type 'dict'> ))

If I add property_type to the alias:

    @EndpointsAliasProperty(setter=roless_set, property_type=Role)

I'm getting server error again ( trace ):

TypeError: Property field must be either a subclass of a simple ProtoRPC field, a ProtoRPC enum class or a ProtoRPC message class. Received Role <label=StringProperty('label'), level=IntegerProperty('level')> .

Is there a way to "convert" EndpointsModel to ProtoRPC message class ? Are there any better solutions for creating models with StructuredProperty using POST data? I couldn't find any examples for this, if someone knows any links, please share (:

UPDATE:

After some digging through source code, I found EndpointsModel.ProtoModel() that can be used to convert ndb.Model to ProtoRPC message class

    @EndpointsAliasProperty(setter=roless_set, property_type=Role.ProtoModel())

This resolves issue with EndpointsAliasProperty workaround, but the problem remains...

Check this repo: https://github.com/zdenulo/epd-error-example . Here I was demonstrating error in endpoints-proto-datastore which in latest version should be fixed. So upgrade in repository to latest endpoints-proto-datastore and you should have working example which similar to what you want to achieve.

Hey Sasxa has there been a fix for this as far as you know? I am currently dealing with the same issue, and I can start a new thread for this discussion if nothing has been found.

Update: created a new issue linked to this one

This issue has been resolved! 此问题已解决! You can check out the issue here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM