Interest Ask! So basically you want to look at the Contact class and find out if there is any other model class that has KeyProperty referencing it; in this example, PhoneNumber (but there can be many).
I think the solution is to ask your users to explicitly add this link when creating the PhoneNumber class.
You can do this easily for your users by providing them with a subclass of KeyProperty that takes care of this; eg.
class LinkedKeyProperty(ndb.KeyProperty): def _fix_up(self, cls, code_name): super(LinkedKeyProperty, self)._fix_up(cls, code_name) modelclass = ndb.Model._kind_map[self._kind] collection_name = '%s_ref_%s_to_%s' % (cls.__name__, code_name, modelclass.__name__) setattr(modelclass, collection_name, (cls, self))
Exactly how you choose a name for the collection and a value for storage is up to you; just put something there that will make it easier for you to click on the link. In the example, a new Contact attribute will be created:
Contact.PhoneNumber_ref_contact_to_Contact == (PhoneNumber, PhoneNumber.contact)
[edited to make the code work and add an example. :-)]
Guido van rossum
source share