可以将重复的属性用作NDB中的常规属性吗?

时间:2017-04-17 11:07:07

标签: google-app-engine app-engine-ndb google-app-engine-python

在Google App Engine的NDB中,假设我需要一个实体:

  • 有时包含字符串
  • 有时包含字符串列表
  • 永远不需要该字段
  • 我从不同时需要两个

鉴于对具有单个元素的重复属性的查询与非重复属性的工作相同,即使我有时将其用作常规字段,也会出现性能差异或具有单个重复字段的任何缺点StringProperty? (撇开不同字段重复和不重复的更好的可读性)

基本上:

import re

text = ("Education shall be directed to the full development of the human personality "
        "and to the strengthening of respect for human rights and fundamental freedoms.")

def search(target, text, context=6):
    # It's easier to use re.findall to split the string, 
    # as we get rid of the punctuation
    words = re.findall(r'\w+', text)

    matches = (i for (i,w) in enumerate(words) if w.lower() == target)
    for index in matches:
        if index < context //2:
            yield words[0:context+1]
        elif index > len(words) - context//2 - 1:
            yield words[-(context+1):]
        else:
            yield words[index - context//2:index + context//2 + 1]

print(list(search('the', text)))
# [['be', 'directed', 'to', 'the', 'full', 'development', 'of'], 
#  ['full', 'development', 'of', 'the', 'human', 'personality', 'and'], 
#  ['personality', 'and', 'to', 'the', 'strengthening', 'of', 'respect']]

print(list(search('shall', text)))
# [['Education', 'shall', 'be', 'directed', 'to', 'the', 'full']]

print(list(search('freedoms', text)))
# [['respect', 'for', 'human', 'rights', 'and', 'fundamental', 'freedoms']]

VS

class model1(ndb.Model):
    str1 = ndb.StringProperty(repeated=True)

感谢。

1 个答案:

答案 0 :(得分:0)

当然是的,但该字段将是一个列表。 如果你不存储任何东西,它将是一个空列表。如果添加一个字符串,它将是一个包含一个值的列表。

请记住,因为此字段将是一个列表,然后添加值只需调用&#34; field.append(yourstring)