The following backlinks of unknown species in NDB

Now I am writing my first RESTful web service in GAE and in the Python 2.7 runtime; I started using the new nido guido API.

However, I'm not sure how to solve a specific case without the implicit backlink function of the source API. If the user-agent requests a specific resource and these resources are removed by 1 degree:

host / api / type / id? Depth = 2

What is the best way to discover a linked set of one-to-many objects in a one-to-many relationship, given that the appearance of the linked object is unknown at design time?

  • I cannot use the replacement request as described in the previous SO request due to the last restriction. The fact that my model is determined at runtime (and therefore not hard-coded) does not allow me to use a query to filter properties for key matching.

  • Ancestors and other heartless queries also arise due to a data warehouse limitation that prevents me from filtering a property without a specified type.

So far, the only idea I came across (besides returning to db api) is to use a transaction between groups to write my own link to "one", or by updating ndb.StringProperty (repeat = True) containing all the related views when an object of a new type is introduced, or the list of keys on "one" ndb.KeyProperty (repeat = True) is simply maintained each time the associated "many" object is written to the data store.

I hope someone more experienced than me can suggest a better approach.

Given the jmort253 suggestion, I will try to enlarge my question with a specific example adapted from the docs:

class Contact(ndb.Expando): """ The One """ # basic info name = ndb.StringProperty() birth_day = ndb.DateProperty() # If I were using db, a collection called 'phone_numbers' would be implicitly # created here. I could use this property to retrieve related phone numbers # when this entity was queried. Since NDB lacks this feature, the service # will neither have a reference to query nor the means to know the # relationship exists in the first place since it cannot be hard-coded. The # data model is extensible and user-defined at runtime; most relationships # will be described only in the data, and must be discoverable by the server. # In this case, when Contact is queried, I need a way to retrieve the # collection of phone numbers. # Company info. company_title = ndb.StringProperty() company_name = ndb.StringProperty() company_description = ndb.StringProperty() company_address = ndb.PostalAddressProperty() class PhoneNumber(ndb.Expando): """ The Many """ # no collection_name='phone_numbers' equivalent exists for the key property contact = ndb.KeyProperty(kind='Contact') number = ndb.PhoneNumberProperty() 
+7
source share
2 answers

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. :-)]

+8
source

Sounds like a good use ndb.StructuredProperty .

+2
source

All Articles