Cómo determinar mediante programación si la propiedad ndb es multivalor

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 whether a property of a ndb.model class is a multivalue property or not.

I suspect that this is due to lack of basic Python skills since the code I have come up with so far shows that the value is “visible”. I am thus not able to grab it. Please help.

from google.appengine.ext import ndb

class House(ndb.Model):
  name = ndb.StringProperty()   
  rooms = ndb.StringProperty(repeated=True) 

print 'Properties:'
for p in House._properties:
  print getattr(House,p)

print '\nRepeated:'
for p in House._properties:
  print getattr(getattr(House,p),'repeated',None)

Esto da como resultado lo siguiente:

StringProperty('rooms', repeated=True)


preguntado el 21 de mayo de 12 a las 19:05

OK, I had a look at the source code and there is an attribute in the Property class named ´_´repeated' that is accessible. It works but I have hunch that the "'_'" means that it is not meant for outside access. -

2 Respuestas

Actually, the underscore-prefixed options se encuentran las the official API. It's a bug that they aren't documented, I will fix that. I explained a little more here: http://code.google.com/p/appengine-ndb-experiment/issues/detail?id=187

In particular, to get the properties of a model, you should use House._properties, not the code that Nick proposed. (In fact, __dict__ may be replaced with slots in future NDB versions.)

Respondido 19 Abr '14, 08:04

Thank you Mr Rossum that was clarifying. - arne s

NDB doesn't currently offer any options to introspect created models; you should definitely presentar un error 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:



getattr(House, 'rooms')._repeated

contestado el 22 de mayo de 12 a las 02:05

Thank you Nick. I have added this as an , el asunto de como se sugiere. - arne s

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.