PynamoDB:如何更新MapAttribute的ListAttribute中的属性

2024-06-16 17:01:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何在Python代码中使用PynamoDB与AWS DynamoDB一起工作。以下是我到目前为止所定义的:

class OfficeEmployeeMap(MapAttribute):
    office_employee_id = NumberAttribute(hash_key=True)
    office_employee_direct_ids = UnicodeSetAttribute()

class Office(Model):
    class Meta:
        table_name = 'OfficeModel'
    office_id = NumberAttribute(hash_key=True)
    employees = ListAttribute(of=OfficeEmployeeMap)

Office.create_table(read_capacity_units=1, write_capacity_units=1) 

下面是我如何附加到映射属性列表(employees)中的:

emp1 = OfficeEmployeeMap(
    office_employee_id=111,
    office_employee_direct_ids={"222"}
)

Office(
    office_id=1
).update(
    actions=[
        Office.employees.set((Office.employees|[]).append([emp1]))
    ]
)

下面是我在新创建的DynamoDB表OfficeModel中看到的内容:

enter image description here

现在我需要向集合office_employee_direct_ids添加另一个值,如333,其中office_employee_id=111,我如何使用PynamoDB来实现它?谢谢你的帮助


Tags: keyidtrueidsemployeehashdynamodbclass