Python 如何使用PynamoDB实例化具有本地二级索引的类模型?
我需要实例化一个使用PynamoDB库的本地二级索引的类模型,但是我得到了错误。我的代码:Python 如何使用PynamoDB实例化具有本地二级索引的类模型?,python,pynamodb,Python,Pynamodb,我需要实例化一个使用PynamoDB库的本地二级索引的类模型,但是我得到了错误。我的代码: class MyIndex(LocalSecondaryIndex): class Meta: projection = AllProjection() org_id = UnicodeAttribute(hash_key=True) user_id = UnicodeAttribute(range_key=True) class MyMod
class MyIndex(LocalSecondaryIndex):
class Meta:
projection = AllProjection()
org_id = UnicodeAttribute(hash_key=True)
user_id = UnicodeAttribute(range_key=True)
class MyModel(Model):
class Meta:
table_name = 'my-table'
region = 'eu-west-1'
aws_access_key_id = '...'
aws_secret_access_key = '...'
org_id = UnicodeAttribute(hash_key=True)
doc_id = UnicodeAttribute(range_key=True)
user_index = MyIndex()
attr_one = NumberAttribute()
attr_two = UnicodeAttribute()
attr_three = NumberAttribute()
这是我第一次在PynamoDB中使用索引(本地或全局)。这些文档实际上没有给出使用索引实例化模型类的示例(我已经看到),只是模型/索引定义,然后是查询示例(我指的是和)
我第一次尝试测试的结果如下:
MyModel(
org_id = my_org_id,
doc_id = my_doc_id,
user_index = MyIndex(my_org_id, 'abc123'),
attr_one = 1,
attr_two = 'foo',
attr_three = 3,
)
…但我在MyIndex
构造函数上遇到以下错误:
TypeError: __init__() takes 1 positional argument but 3 were given
删除第一个参数my_org_id
时出现了上面的错误,…但给出了2个
。我还尝试了user\u index='abc123'
和user\u id='abc123'
,但在这两种情况下,我都得到了ValueError:指定的属性user*不存在
有人能告诉我这是如何工作的,或者给我指出一个工作示例的方向吗?我刚刚从示例中意识到,LSI中使用的排序键也必须指定为使用LSI的表上的属性,以便使其工作,例如
class MyModel(Model):
class Meta:
table_name = 'my-table'
region = 'eu-west-1'
aws_access_key_id = '...'
aws_secret_access_key = '...'
org_id = UnicodeAttribute(hash_key=True)
doc_id = UnicodeAttribute(range_key=True)
user_index = MyIndex()
user_id = UnicodeAttribute() ### this was missing