How to POST ndb.StructuredProperty?

2024/10/13 8:21:26

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...

Answer

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.

https://en.xdnf.cn/q/118101.html

Related Q&A

Issue computing difference between two csv files

Im trying to obtain the difference between two csv files A.csv and B.csv in order to obtain new rows added in the second file. A.csv has the following data.acct ABC 88888888 99999999 ABC-G…

How do I display an extremly long image in Tkinter? (how to get around canvas max limit)

Ive tried multiple ways of displaying large images with tkinterreally long image No matter what Ive tried, there doesnt seem to be any code that works. The main issue is that Canvas has a maximum heigh…

NoneType has no attribute IF-ELSE solution

Im parsing an HTML file and searching for status of order in it. Sometimes, status doesnt exist, so BeautifulSoup returns NoneType, when Im using it. To solve this problem I use if-else statement, but …

looking for an inverted heap in python

Id like to comb off the n largest extremes from a timeseries. heapq works perfectly for the nlargestdef nlargest(series, n):count = 0heap = []for e in series:if count < n:count+=1hp.heappush(heap, e…

Concatenating Multiple DataFrames with Non-Standard Columns

Is there a good way to concatenate a list of DataFrames where the columns are not regular between DataFrames? The desired outcome is to match up all columns that are a match but to keep the ones that …

Python Conditionally Add Class to td Tags in HTML Table

I have some data in the form of a csv file that Im reading into Python and converting to an HTML table using Pandas.Heres some example data:name threshold col1 col2 col3 A 10 12 9 13…

Sort a dictionary of dictionaries python

I have a dictionary of dictionaries like the followingd = {hain: {facet: 1, wrapp: 1, chinoiserie: 1}, library: {sconc: 1, floor: 1, wall: 2, lamp: 6, desk: 1, table: 1, maine: 1} }So, I want to rever…

How can I get python generated excel document to correctly calculate array formulas

I am generating some excel files with python using python 3.6 and openpyxl.At one point I have to calculate standard deviations of a subsection of data. In excel this is done with an array formula. Wri…

Unable to locate element in Python Selenium

Im trying to locate an element using python selenium, and have the following code:zframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[5]") driver.switch_to.frame(zframe) find…

How to import a variable from a different class

I have an instance of a class that i set the value to self.world inside a class named zeus inside a module named Greek_gods. and i have another class names World inside a module name World.How can i te…