Google App Engine ndb equivalent for modelname_set (backreference property)
Asked Answered
I

1

10

Is there an equivalent for modelname_set (a back-referenced property) in Google App Engine's NDB?

In the old DB a Model entity had described the back-reference property as:

The name of the back-reference property defaults to modelname_set (with the name of the model class in lowercase letters, and "_set" added to the end), and can be adjusted using the collection_name argument to the ReferenceProperty constructor.

I noticed this property does not seem to exist with NDB db.Model instances.

Does NDB have an equivalent to the back-reference property?

Ixtle answered 29/3, 2012 at 0:26 Comment(0)
J
13

There is no direct back-reference properties in NDB because NDB doesn't quite use the same paradigm as the original datastore client. You would use a KeyProperty for your forward reference and then use a query for everything that has that KeyProperty set for your back reference.

class Comment(ndb.Model)
    source = ndb.KeyProperty()

qry = Comment.query().filter(source=ndb.Key('Source', 'Sandy'))
Jemimah answered 29/3, 2012 at 3:37 Comment(2)
Thanks Michael. Just curious, what purpose does 'Sandy' have in your example?Ixtle
'Sandy' is just the (user-assigned) ID of the key, just like 'Source' is the kind.Kauffman

© 2022 - 2024 — McMap. All rights reserved.