How to solve some sort of chicken egg relation within ndb.Models?

1.7k Views Asked by At

I have two entities (events and users). Each user has several events, but I don't want them to be stored within a StructuredProperty, because in future it should be possible to have multiple creators/admins. Now I have the problem, that User needs the Event Class for definition and vice versa. How can I implement the intended structure?

Two models with mutual relations.

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)

Error Message:

NameError: name 'User' is not defined

EDIT 1: I changed the kind to a string, containing the class name, like Greg suggested it. But it does not work too.

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)

Now I receive the following stack trace:

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 []
3

There are 3 best solutions below

0
On

You could specify the key properties without the kind parameter (it is optional) and then do a manual check in your constructor or a pre-put hook or something like that -- or maybe not even worry about the 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")

The actual syntax will probably be slightly different. Feel free to edit.

2
On

You can not create such references to the entities. Here is somo solutions: 1. You must use normal StringProperty for Event.creator or other id for User instance 2. Remove evens from class User - you can reach to evens by index on class Events 3. Use third entity model like this:

class EventCreator(EndpointsModel):
    creator = ndb.KeyProperty(kind=User)
    event =  ndb.KeyProperty(kind=Event)

and from class User remove creator & from class Event remove

5
On

You can use strings in the KeyProperty constructor to refer to kinds that don't have a model definition:

class Event(ndb.Model):
  participants = ndb.KeyProperty(kind='User', repeated=True)