将数据上的对象合并到数据库

时间:2015-01-12 10:32:34

标签: python json django merge put

我在尝试从数据库中的对象中删除自定义属性时出现问题。

我写了一个方法来合并db中已经存在的对象和新发布的对象。 我传递了对象类,其中包含一个nonupdatable,nonoptional,notempty和nonpublic属性的列表,如下所示:

non_optional_attributes = [IN]
non_updatable_attributes = {ID, TYPE, IN}

假设我有这样的db_object:

#DB_Object
{
   IN:"12345",
   TYPE:"tree",
   ID:"T-12345",
   "customAttribute":"value"
}

我想通过put删除customAttribute,并将发布的数据传递给我的merge函数,只需发布​​没有像这样的属性(只是将它留下):

#New_Object
{
   IN:"12345",
   TYPE:"tree",
   ID:"T-12345"
}

以下是应该从db_object中删除缺少密钥的部分:

for r_key in deepcopy(result_obj):
                if r_key in db_obj and key not in new_obj and \
                   r_key not in cls.non_empty_attributes and \
                   r_key not in cls.non_optional_attributes and \
                   r_key not in cls.non_updatable_attributes and \
                   r_key not in cls.non_public_response_keys:

                    del result_obj[key]

再一次了解整个功能:

@classmethod
    def merge_model_object(cls, db_obj, new_obj):
        # validate new_obj first
        cls(new_obj)

        renamed_attrs = cls.get_renamed_attrs()

        # merge: use non_updatable or missing keys from db_obj
        result_obj = deepcopy(db_obj)
        for key, value in new_obj.items():
            if key not in cls.non_updatable_attributes:  # only update if allowed

                # revert values to ids if they have been resolved to objects before
                if key in cls.resolved_attributes or key in renamed_attrs:
                    reverted_attribute = cls.revert_resolved_attribute(value)
                    if reverted_attribute is not None:
                        result_obj[renamed_attrs.get(key, key)] = reverted_attribute

                else:
                    result_obj[key] = value

            elif key in db_obj and db_obj[key] != new_obj[key]:
                if key not in cls.resolved_attributes:
                    raise exceptions.ChangingNonUpdatableAttribute(key)

            for r_key in deepcopy(result_obj):
                if r_key in db_obj and key not in new_obj and \
                   r_key not in cls.non_empty_attributes and \
                   r_key not in cls.non_optional_attributes and \
                   r_key not in cls.non_updatable_attributes and \
                   r_key not in cls.non_public_response_keys:

                    del result_obj[key]

        return result_obj

当我在那里放置一个断点并检查我的自定义键会发生什么时,它只是跳过删除。我已经再次检查过,密钥不在限制列表中。所以我不明白为什么它没有被删除。 有人有想法吗?

此外还有一个屏幕截图,限制列表在运行时得到的值:

enter image description here

0 个答案:

没有答案