自制,一个简单的封装,让leancloud 的数据模型和查询写起来不那么让人想锤墙。

这是项目地址 better-leancloud-storage-python1

这是关于这个项目的博客文章3

还不够到位,还请大家大家多多指教~能参与的话就更棒啦~

下面上个例子看看改进的查询写法。

# 使用 leancloud-sdk 编写
from datetime import datetime
from leancloud import Object, LeanCloudError

Product = Object.extend('Product')
try:
    # 查找叫做 MyProduct,价格比10高,在18年8月1日之前创建的商品
    product = Product.query.equal_to('name', 'MyProduct')\
        .greater_than_or_equal_to('price', 10)\
        .less_than_or_equal_to('createdAt', datetime(year=2018,month=8,day=1))\
        .first()
except LeanCloudError as exc:
    if exc.code == 101:
        product = None
    else:
        raise

# 使用 better-leancloud-storage-python 编写
from datetime import datetime
from better_leancloud_storage.storage.models import Model
from better_leancloud_storage.storage.fields import Field

class Product(Model):
    name = Field()
    price = Field()
    created_at =Field()

product = Product.query()\
    .filter_by(name='MyProduct')\
    .filter(Product.price > 10,
            Product.created_at < datetime(year=2018,month=8,day=1))\
            .first()
3 人赞了这个帖子.

吓,忘记换成私人账号了

更新了新功能。

1,现在模型声明支持使用默认值了。
2,现在查询可以使用order_by了。

order_by的使用方式如下

class MyModel(Model):
    field = Field()

MyModel.query().order_by(Model.field.asc).first()

顺便加入了 codecov 的 badge,覆盖率 90%~

已提交pypi,可以通过 pip install leancloud-better-storage来添加为依赖了。