두 개의 엔티티 (이벤트 및 사용자)가 있습니다. 각 사용자는 여러 이벤트가 있지만 StructuredProperty 내에 이러한 이벤트가 저장되는 것을 원하지 않습니다. 왜냐하면 나중에 여러 작성자/관리자를 가질 수 있어야하기 때문입니다. 이제는 사용자가 정의 할 이벤트 클래스가 필요하며 그 반대도 마찬가지입니다. 의도 한 구조를 어떻게 구현할 수 있습니까?ndb.Models에서 닭고기 달걀 관계를 해결하는 방법은 무엇입니까?
상호 관계가있는 두 모델입니다.
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: name 'User' is not defined
편집 1 : 그렉을 제안처럼 나는, 클래스 이름을 포함하는 문자열로 종류를 변경했습니다. 그러나 그것은 또한 작동하지 않습니다.
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 []
kind를 문자열로 지정하여 순환 의존성을 만들 수있는 다른 클래스를 참조하는 KeyProperty를 만들거나 정의 할 수 있습니다. 다른 대답을 참조하십시오. –
니스, 나도이 기능을 모른다. 고맙습니다. 나는 이것이 최선의 방법이라는 것을. – jacek2v