app-engine-rest-server to raise KeyError("name %s already used" % model_name)
I'm playing with the project appengine-rest-server to create the REST webservices for all the existing models. I got a strange error, the first time I query the browser: http://localhost:8080/rest/metadata/user, it 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 refreshing the page, gives me 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 the explanation on why it happens? Restarting the server, run on the browser again I get the xml result, but refreshing causes the error. Is it a bug in the appengine-rest-server application or it is in my code? My helloworld application is available for download here.
Since you have a main()
function, the App Engine caches your module and imports. Hence within the same runtime, you may call add_models()
more than once. If you move the rest
initialization code to module level (or into a function that gets called once during module initialization, not during every call to main()
) it should work.
精彩评论