google app engine - How to query with a list object on repeated string property in ndb -


with google app engine, use ndb manange data. have entity use repeated string property save list of string values. how can query list object equal property value?

what meant is, entity is

class question(ndb.model):     question = ndb.stringproperty(required=true)     choices = ndb.stringproperty(repeated=true) 

and want query this:

question = "the question" choices = ["choice1", "choice2", ..., "choice6"] question.query(question.question==question, question.choices==choices) 

when have repeated property, there no way perform query searching exact equality of list.

for example, question entity with:

question = "a question" choices = ["a", "b", "c"] 

will match all of following query

question.query(question.choices=="a") question.query(question.choices=="b") question.query(question.choices=="c") 

solution 1: computed property

if want write query entire list equal, use computed property stringified version of repeated property:

class question(ndb.model):     question = ndb.stringproperty(required=true)     choices = ndb.stringproperty(repeated=true)     choices_list = ndb.computedproperty(lambda self: return ",".join(self.choices)) 

then query using choices_list:

choices = [...] question.query(question.choices_list==",".join(choices)) 

of course, match if choices match (including order).


solution 2: using and

you use , query:

choices = [...] questions.query(                 ndb.and(question.choices==choices[0],                 ....,                 question.choices==choices[n])) # in programmatic way 

this solution has upside ordering of list not matter, may result in false positives, query ["a", "b"] match entity choices ["a", "b", "c"].


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -