Duplicate Query AppEngine to create filter options without affecting the underlying query

In my AppEngine project, I need to use a specific filter as a base and then apply various different additional filters to the end, getting different result sets separately. eg:.

base_query = MyModel.all().filter('mainfilter', 123)

      

Then I need to use the results of the various subqueries separately:

subquery1 = basequery.filter('subfilter1', 'xyz')
#Do something with subquery1 results here

subquery2 = basequery.filter('subfilter2', 'abc')
#Do something with subquery2 results here

      

Unfortunately, "filter ()" affects the state of the Queryqueryquery instance, rather than just returning the modified version. Is there a way to duplicate the Query object and use it as a base? Perhaps there is a standard Python way to trick an object that can be used?

The additional filters are actually applied dynamically in different views in the wizard, and they use the "running amount" of the request in their branch to gauge whether additional questions should be asked.

Obviously, I could go around the rudimentary stacks of filtering criteria, but I would rather use the query itself if possible, since it adds simplicity and elegance to the solution.

+2


a source to share


2 answers


Not officially approved (e.g. can't break) a way to do this. Simply re-creating the request from parameters when you need it is your best bet.



+2


a source


As Nick said, you'd better re-create the query, but you can still avoid repeating it. A good way to do this would be:



#inside a request handler

def create_base_query():
  return MyModel.all().filter('mainfilter', 123)

subquery1 = create_base_query().filter('subfilter1', 'xyz')
#Do something with subquery1 results here

subquery2 = create_base_query().filter('subfilter2', 'abc')
#Do something with subquery2 results here

      

+2


a source







All Articles