如何以编程方式确定ndb属性是否为多值 [英] How to programmatically determine if ndb property is multivalue

查看:87
本文介绍了如何以编程方式确定ndb属性是否为多值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从Datastore转换为ndb,并在xml导入例程中遇到问题。问题是我无法以编程方式确定ndb.model类的属性是否为多值属性。



我怀疑这是由于缺乏基本的Python技能,因为我到目前为止所提供的代码表明该值是可见的。因此我无法抓住它。

  from google.appengine.ext import ndb 

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

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

打印'\\\
Repeated:'
for House._properties:
print getattr(getattr(House,p) ,'repeated',None)

结果如下:

 属性:
StringProperty('rooms',repeated = True)
StringProperty('name')

重复:



解决方案

实际上,带下划线的前缀选项官方API。这是一个错误,他们没有记录,我会解决这个问题。我在这里解释了一些: http:// code .google.com / p / appengine-ndb-experiment / issues / detail?id = 187

特别要获取模型的属性,你应该使用House._properties,而不是Nick提出的代码。 (事实上​​, __ dict __ 可能会替换为未来NDB版本的插槽。)


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)

This results in the following:

Properties:
StringProperty('rooms', repeated=True)
StringProperty('name')

Repeated:
None
None

解决方案

Actually, the underscore-prefixed options are 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.)

这篇关于如何以编程方式确定ndb属性是否为多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆