使用pynamodb库创建dynamodb记录时,“NoneType”对象不可编辑

2024-06-16 16:04:34 发布

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

我正在尝试创建记录并将其保存到DynamoDB

ServiceStateModel(
    instance_id=service_request["instance_id"],
    plan_id=service_request["plan_id"],
    service_id=service_request["service_id"],
    space_id=service_request["space_guid"],
    organization_id=service_request["organization_guid"],
    last_operation={
        'name': ServiceOperationEnum.PROVISION,
        'state': ServiceStateEnum.IN_PROGRESS},
    cloud_foundry={
        'apps': apps,
        'services': services
    })\
    .save()

其中appsservices看起来像这样

apps = [CloudFoundryEntityAttribute(entity_id=app, state=ServiceStateEnum.IN_PROGRESS)
        for app in target_profile['apps'].keys()]
services = [CloudFoundryEntityAttribute(entity_id=service, state=ServiceStateEnum.IN_PROGRESS)
            for service in target_profile['services'].keys()]

我在日志中检查它们,它们不是空的或None,而是包含我期望的数据。但是,当我尝试执行save时,它会失败并出现错误

File "/.../cloud.py", line 243, in deploy
    'services': services
File "/usr/local/lib/python3.7/site-packages/pynamodb/models.py", line 409, in save
    data = self._get_connection().put_item(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/table.py", line 139, in put_item
    return_item_collection_metrics=return_item_collection_metrics)
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/base.py", line 967, in put_item
    return_item_collection_metrics=return_item_collection_metrics
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/base.py", line 859, in get_operation_kwargs
    operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key, key=key))
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/base.py", line 784, in get_identifier_map
    return tbl.get_identifier_map(hash_key, range_key=range_key, key=key)
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/base.py", line 207, in get_identifier_map
    self.hash_keyname: {
File "/usr/local/lib/python3.7/site-packages/pynamodb/connection/base.py", line 101, in hash_keyname
    for attr in self.data.get(KEY_SCHEMA):
    TypeError: 'NoneType' object is not iterable

我的model定义如下

class CloudFoundryEntityAttribute(MapAttribute):
    entity_id = UnicodeAttribute()  # effectively a name
    state = UnicodeAttribute()


class CloudFoundryAttribute(MapAttribute):
    services = ListAttribute(of=CloudFoundryEntityAttribute)
    apps = ListAttribute(of=CloudFoundryEntityAttribute)


class CloudFoundryLastOperationAttribute(MapAttribute):
    name = UnicodeAttribute()
    state = UnicodeAttribute()


class ServiceStateModel(Model):
    class Meta:
        table_name = Config.AWS_DYNAMODB_TABLE_NAME
        region = Config.AWS_DEFAULT_REGION
        aws_access_key_id = Config.AWS_ACCESS_KEY_ID
        aws_secret_access_key = Config.AWS_SECRET_ACCESS_KEY

    instance_id = UnicodeAttribute(hash_key=True)
    plan_id = UnicodeAttribute()
    service_id = UnicodeAttribute()
    space_id = UnicodeAttribute()
    organization_id = UnicodeAttribute()
    cloud_foundry = CloudFoundryAttribute()
    last_operation = CloudFoundryLastOperationAttribute()

怎么了?我想可能是我的属性定义


Tags: keyinpyidgetlibpackagesusr