Pynamodb - 动态JSON序列化

时间:2018-05-30 22:43:52

标签: python amazon-dynamodb

我有一个复合模型,如下所示: -

class Parser(MapAttribute):
    ParserType = UnicodeAttribute()
    MailBody = UnicodeAttribute()
    BestTemplate = UnicodeAttribute(null=True)
    ParseStatus = ParseStatus()
    ParsedOutput = JSONAttribute()


class OMPStatusModel(Model):
    """
    PynamoDB Model handling table OMPStatus
    """


    class Meta:
        table_name = 'OMPStatus'
        region = 'us-east-1'
    SNSMessageID = UnicodeAttribute(hash_key=True)
    CreatedDateTime = UTCDateTimeAttribute()
    UpdatedDateTime = UTCDateTimeAttribute(null=True)
    CompletedDateTime = UTCDateTimeAttribute(null=True)
    ProcessStatus = UnicodeAttribute()
    ErrorDetail = UnicodeAttribute(default="Not set")
    SES = SES(null=True)
    SNS = SNS(null=True)
    Parser = Parser(null=True)

这很有效,除了ParsedOutput存储为DynamoDB中的键值对列表。我想让支持团队更容易阅读,并希望将其分解为自己的属性列表 - 假设它们都可以是UnicodeAttribute()以供讨论。

我在想我应该能够做到这样的事情: -

class MyMapAttribute(MapAttribute):
    my_internal_map = MapAttribute()


class Parser(MapAttribute):
    ParserType = UnicodeAttribute()
    MailBody = UnicodeAttribute()
    BestTemplate = UnicodeAttribute(null=True)
    ParseStatus = ParseStatus()
    ParsedOutput = MyMapAttribute(default = {})

然后执行以下操作: -

OMPStatusModel.update(actions=[OMPStatusModel.SES.set(kwargs[key]),
                          OMPStatusModel.UpdatedDateTime.set(datetime.now())])

并不是因为它不能正常工作。 有什么指针吗?

1 个答案:

答案 0 :(得分:0)

最终,这归结于我没有将正确的物体传递给地图。一旦我这样做,它工作得很好。