I am using PynamoDB and trying to genrate a dynamic query/scan on attributes from a flask request. Assuming the following model:
class Test(Model):
class Meta:
table_name = "test"
region = "us-west-2"
a = NumberAttribute(hash_key=True)
b = NumberAttribute(range_key=True)
c = UnicodeAttribute()
I have this bolted to Flask, so that when a user sends a request with query arguments such as ?a=123&b=456 I can turn this into a filter within PynamoDB to return results which match records with those two filters.
The following works for a single attribute:
>>> query = operator.eq(Test.a, 123)
>>> list(Test.scan(query))
[Test<4426599961>]
I am looking to be able to bolt this onto a dynamic number of attributes (beyond a,b and c for example)
I tried turning this into a tuple, but that seems to break with the following error:
>>> query = (operator.eq(Test.a, 123), operator.eq(Test.b, 456))
>>> list(Test.scan(*query))
but I get the following error:
Invalid type for parameter Segment, value: b = {'N': '1'}, type: <class 'pynamodb.expressions.condition.Comparison'>, valid types: <type 'int'>, <type 'long'>
Any ideas on how I can construct a dynamic filter to pass into the PynamoDB scan method?