Duplicate an AppEngine Query object to create variations of a filter without affecting the base quer

Posted by Steve Mayne on Stack Overflow See other posts from Stack Overflow or by Steve Mayne
Published on 2010-05-08T10:40:31Z Indexed on 2010/05/08 10:48 UTC
Read the original article Hit count: 245

In my AppEngine project I have a need to use a certain filter as a base then apply various different extra filters to the end, retrieving the different result sets separately. e.g.:

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

Then I need to use the results of various sub queries 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 basequery Query instance, rather than just returning a modified version. Is there any way to duplicate the Query object and use it as a base? Is there perhaps a standard Python way of duping an object that could be used?

The extra filters are actually applied by the results of different forms dynamically within a wizard, and they use the 'running total' of the query in their branch to assess whether to ask further questions.

Obviously I could pass around a rudimentary stack of filter criteria, but I'd rather use the Query itself if possible, as it adds simplicity and elegance to the solution.

© Stack Overflow or respective owner

Related posts about python

Related posts about google-app-engine