Maddy Byahoo Maddy Byahoo - 1 year ago 80
Python Question

How to query the property of a property in Google App Engine?

I have two NDB Model classes, Book and Author. Author has a StringProperty "name" and an auto_add_now "date", and Book has a PickleProperty "authors" which is a list of objects of type Author.

I want to make a query and receive a list of all Books who have currentAuthor in their list of authors (matching "name" and ignoring "date"). I don't think I can use .IN directly because the dates might be different. I was playing around with setting date=None for testing purposes (as it seemed to be None anyway for all Author objects? is this because I'm running it locally?) and then writing

books = Book.query(Book.authors.IN([currentAuthor])).fetch()

but that returns an empty list.

Answer Source

It won't work with a PickleProperty

From the docs

PickleProperty - Value is a Python object (such as a list or a dict or a string) that is serializable using Python's pickle protocol; the Datastore stores the pickle serialization as a blob. Unindexed by default.

Even if you did have it indexed you won't acheive an IN query as it wouldn't know have indexed the entities in the pickle as separate indexed values.

You should use a repeated StructuredProperty, KeyProperty or StringProperty for the authors. Then you could perform the query as intended .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download