Appengine - Possible to get an entity using only k

2019-04-10 22:06发布

I want to be able to have a view that will act upon a number of different types of objects

all the view will get is the key string eg:

agpwb2xsdGhyZWFkchULEg9wb2xsY29yZV9hbnN3ZXIYAww

without knowing the model type, is it possible to retrieve the entity from just that key string?

thanks

2条回答
何必那么认真
2楼-- · 2019-04-10 22:55

If you design your models so they all use a common superclass it should be possible to retrieve your objects by using something like:

entity = CommonSuperclass.get('agpwb2xsdGhyZWFkchULEg9wb2xsY29yZV9hbnN3ZXIYAww')
查看更多
疯言疯语
3楼-- · 2019-04-10 23:00

No superclassing required, just use db.get():

from google.appengine.ext import db
key_str = 'agpwb2xsdGhyZWFkchULEg9wb2xsY29yZV9hbnN3ZXIYAww'
entity = db.get(key_str)
查看更多
登录 后发表回答