StructuredProperty 和 StringProperty 的问题

Problems with StructuredProperty and StringProperty

我正在 Google App Engine 完成最后的学位工作,但是我在尝试这个时遇到了问题:

class Predicate(ndb.Model):
    name = ndb.StringProperty()
    parameters = ndb.JsonProperty()

class State(ndb.Model):
    predicates = ndb.StructuredProperty(Predicate, repeated=True)

class Action(ndb.Model):
    name = ndb.StringProperty()
    parameters = ndb.StringProperty(repeated=True)
    preconditions = ndb.StructuredProperty(Predicate, repeated=True)
    predicatesToAdd = ndb.StructuredProperty(Predicate, repeated=True)
    predicatesToDel = ndb.StructuredProperty(Predicate, repeated=True)

class Plan(ndb.Model):
    plan = ndb.StructuredProperty(Predicate, repeated=True)

class Problem(ndb.Model):
    initialState = ndb.StructuredProperty(Predicate)
    goalState = ndb.StructuredProperty(Predicate)
    actions = ndb.StructuredProperty(Action, repeated=True)

我收到这个错误:

TypeError: This StructuredProperty cannot use repeated=True because its model class (Predicate) contains repeated properties (directly or indirectly).

StructuredProperty,如果包含重复项,则无法复制另一个 StructuredProperty。但是我需要这种结构模型。我该如何解决?

抱歉我的英语不好:(


我使用 LocalStructuredProperty 解决了这个问题,但我认为它根本行不通

您的设计存在问题,ndb 不允许嵌套的重复属性。换句话说,你不能有一个重复的结构化 属性,它又有自己的重复 属性。如果从参数 属性 中删除 repeated=True,它将起作用。

您需要重新考虑您的设计以解决此问题。一种可能的解决方案可能是使用 JsonProperty 作为参数,并将字符串列表存储为 JSON 字符串。当然,您将无法查询它们,但它可能会根据您的要求解决。