How to programmatically determine if ndb property is multivalue

后端 未结 2 1531
情深已故
情深已故 2021-01-14 09:01

I am translating an app from Datastore to ndb and have encountered a problem in the xml import routine. The problem is that I am not able to programmatically determine whet

2条回答
  •  梦谈多话
    2021-01-14 09:29

    NDB doesn't currently offer any options to introspect created models; you should definitely file a bug about this. In the meantime, poking into the object's internals is the only way to go about it. Be warned that this is very brittle, as internal implementation details can and will change at any time.

    You can get a list of properties like this:

    properties = [(k, v) for k, v in House.__dict__.items() if isinstance(v, ndb.Property)]
    

    You can determine if a property is repeated by accessing the _repeated internal attribute on an instance - but see my disclaimer above for why this is probably a bad idea:

    House.rooms._repeated
    

    OR

    getattr(House, 'rooms')._repeated
    

提交回复
热议问题