python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

query.get(object_id) should throw an Exception when object not found

Open weakish opened this issue 4 years ago • 2 comments

This is consistent with query.first.

Thank artsungames for bringing this to our attention.

weakish avatar Apr 06 '21 07:04 weakish

object_id 在云存储中不存在的时候,会返回一个本地构造的 object,可以通过 created_at 是否被设置来察觉到。 当前我们自己的项目里做了这样的 patch:

from leancloud import Query as OriginalQuery


class Query(OriginalQuery):
    def get(self, object_id):
        obj = super().get(object_id)

        # 此处也可以 raise 101,大家可以按自己的习惯来
        return obj if getattr(obj, "created_at", None) else None


def patch_leancloud():
    import leancloud

    leancloud.Query = Query

Ma233 avatar Apr 14 '21 12:04 Ma233

可以通过 crerated_at 是否被设置来察觉到

是的,其实 SDK 提供了一个 is_existed 方法(这个方法在实现上也是检查 created_at

weakish avatar Apr 15 '21 03:04 weakish