我有两个实体(事件和用户)。每个用户都有几个事件,但我不希望它们存储在StructuredProperty中,因为将来应该可以有多个创建者/管理员。现在我遇到了问题,用户需要事件类进行定义,反之亦然。我该如何实现预期的结构?
两种相互关系的模型。
class Event(EndpointsModel):
_message_fields_schema = ("id", "name", "creator",
"datetime", "place", "category", "participants")
creator = ndb.KeyProperty(kind=User)
participants = ndb.KeyProperty(kind=User, repeated=True)
name = ndb.StringProperty()
datetime = ndb.DateTimeProperty(auto_now_add=True)
place = ndb.GeoPtProperty()
category = ndb.StringProperty(choices=('all', 'drinking'))
class User(EndpointsModel):
_message_fields_schema = ("id", "name", "password", "events")
name = ndb.StringProperty()
password = ndb.StringProperty()
events = ndb.KeyProperty(kind=Event, repeated=True)
def create_event(self, e_name, e_datetime, e_place, e_category):
event = Event(name=e_name, creator = self.key, datetime=e_datetime, place=e_place, category=e_category)
event.put()
self.events.append(event)
self.put()
def get_events(self):
return ndb.get_multi(self.events)
错误讯息:
NameError:名称'User'未定义
编辑1: 我将类型更改为包含类名的字符串,就像Greg建议的那样。但它也不起作用。
class Category(EndpointsModel):
_message_fields_schema = ("id", "name", "parent")
name = ndb.StringProperty()
parent = ndb.KeyProperty(kind='Category', default=None)
class Event(EndpointsModel):
_message_fields_schema = ("id", "name", "creator", "datetime",
"place", "category")
participants = ndb.KeyProperty(kind='User', repeated=True)
creator = ndb.KeyProperty(kind='User')
name = ndb.StringProperty()
datetime = ndb.DateTimeProperty(auto_now_add=True)
place = ndb.GeoPtProperty()
category = ndb.KeyProperty(Category)
class User(EndpointsModel):
_message_fields_schema = ("id", "name", "password")
name = ndb.StringProperty()
password = ndb.StringProperty()
events = ndb.KeyProperty(Event, repeated=True)
现在我收到以下堆栈跟踪:
ERROR 2014-01-21 09:38:39,764 service.py:191] Encountered unexpected error from ProtoRPC method implementation: BadValueError (Expected Key, got [])
Traceback (most recent call last):
File "/home/chris/Downloads/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
response = method(instance, request)
File "/home/chris/Downloads/google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1321, in invoke_remote
return remote_method(service_instance, request)
[...]
value = self._call_shallow_validation(value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1227, in _call_shallow_validation
return call(value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1274, in call
newvalue = method(self, value)
File "/home/chris/Downloads/google_appengine/google/appengine/ext/ndb/model.py", line 1927, in _validate
raise datastore_errors.BadValueError('Expected Key, got %r' % (value,))
BadValueError: Expected Key, got []
答案 0 :(得分:3)
您可以在KeyProperty构造函数中使用字符串来引用没有模型定义的类型:
class Event(ndb.Model):
participants = ndb.KeyProperty(kind='User', repeated=True)
答案 1 :(得分:2)
您无法创建对实体的此类引用。这是somo解决方案: 1.必须为Event.creator使用普通的StringProperty或者为User实例使用其他id 2.从类User中删除evens - 您可以通过类Events上的索引达到均衡 3.使用第三个实体模型:
class EventCreator(EndpointsModel):
creator = ndb.KeyProperty(kind=User)
event = ndb.KeyProperty(kind=Event)
并从课程用户删除创作者&来自班级活动删除
答案 2 :(得分:1)
您可以在没有kind参数的情况下指定键属性(它是可选的),然后在构造函数或预置挂钩或类似的东西中进行手动检查 - 或者甚至不用担心这种类型:
class Event(EndpointsModel):
creator = ndb.KeyProperty()
# Constructor option
def __init__(self, *args, **kwargs):
super(Event, self).__init__(*args, **kwargs)
if 'creator' in kwargs and kwargs['creator'] != 'User':
raise Exception('oh no')
# Hook option
_pre_put_hook(self):
if self.creator and self.creator.kind() != 'User':
raise Exception("oh no")
实际语法可能略有不同。随意编辑。