App-engine-rest-server to raise KeyError ("% s name is already in use"% model_name)
I am playing around with the ap pengine-rest-server project to create REST web services for all existing models. I have a weird error, the first time I ask the browser: http: // localhost: 8080 / rest / metadata / user , this gives me the result:
<xs:schema>
−
<xs:element name="user">
−
<xs:complexType>
−
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="key" type="xs:normalizedString"/>
<xs:element maxOccurs="1" minOccurs="0" name="surname" type="xs:string"/>
<xs:element maxOccurs="1" minOccurs="0" name="firstname" type="xs:string"/>
<xs:element maxOccurs="1" minOccurs="0" name="ages" type="xs:long"/>
<xs:element maxOccurs="1" minOccurs="0" name="sex" type="xs:boolean"/>
<xs:element maxOccurs="1" minOccurs="0" name="updatedDate" type="xs:dateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="createdDate" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But when you refresh the page, you get this error:
Traceback (most recent call last):
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3185, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3128, in _Dispatch
base_env_dict=env_dict)
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 515, in Dispatch
base_env_dict=base_env_dict)
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2387, in Dispatch
self._module_dict)
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2297, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "/Users/foo/Documents/AppEngine/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2195, in ExecuteOrImportScript
script_module.main()
File "/Users/foo/Documents/AppEngine/helloworld/main.py", line 48, in main
rest.Dispatcher.add_models({"user": UserModel})
File "/Users/foo/Documents/AppEngine/helloworld/rest/__init__.py", line 845, in add_models
cls.add_model(model_name, model_type)
File "/Users/foo/Documents/AppEngine/helloworld/rest/__init__.py", line 863, in add_model
raise KeyError("name %s already used" % model_name)
KeyError: 'name user already used'
Can someone give me an explanation as to why this is happening? Restart the server, run it in the browser again. I am getting the xml result, but the update throws an error. Is this a bug in the appengine-rest-server application or in my code? The helloworld app is available for download here .
a source to share
Since you have a function main()
, App Engine caches your module and imports. Therefore, you can call add_models()
more than once within the same runtime . If you move your initialization code rest
to the module level (or to a function that is called once during module initialization, not every call main()
), it should work.
a source to share