I am trying to write a restful service of my own for google app engine. I am dynamically fetching form data and saving the model. To get the attribute list , I am doing

Contact.__properties
{'name': StringProperty('name', required=True), 'email': StringProperty('email', required=True)}

I can do this to find the type of the attribute

(Pdb) type(Contact._properties['name']) is ndb.StringProperty
True

But how can I find if it is required = True or False

What is the type of the output of Contact._properties, how do I decipher it .

有帮助吗?

解决方案

Although poorly documented, there is an introspection API for NDB models; simply use the name of the configuration option with an underscore prefixed:

Contact._properties['name']._required

is the value of the required option.

The underscores do not signify 'privacy' here but are used to avoid clashing with names from the model itself (e.g. if name were a StructuredProperty referencing another model with a required property you wouldn't otherwise be able to access the required option).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top