V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dracarysX
V2EX  ›  Python

一款 no-sql 风格的 Rest API 查询框架,目前实现 Django orm 和 peewee。

  •  
  •   dracarysX · 2017-06-28 16:47:32 +08:00 · 2170 次点击
    这是一个创建于 2465 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RestfulAPI 解析

    实现一款 no-sql 风格的参数解析框架rest-query,(like: /?select=id,name,author{id,name,school{*}})&id=gte.20&author.name=wwxiong&order=id.desc)。

    目前实现了Django ORMPeewee ORM

    Django DEMO

    定义模型

    class Author(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=50)
        age = models.IntegerField()
    
    
    class Book(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=50)
        author = models.ForeignKey(Author)
    

    定义试图

    from django.views.generic import ListView
    from django.views.generic.detail import DetailView
    from django_rest_query import RestQueryDetailViewMixin, RestQueryListViewMixin
    from .models import Author, Book
    
    class BookDetail(RestQueryDetailViewMixin, DetailView):
        model = Book
    
    
    class BookList(RestQueryListViewMixin, ListView):
        model = Book
    
    
    class AuthorDetail(RestQueryDetailViewMixin, DetailView):
        model = Author
    
    
    class AuthorList(RestQueryListViewMixin, ListView):
        model = Author
    

    这样我们就实现了AuthorBook的查询 API 了。

    API

    查询姓名为dracarysX的作者,并只返回id, name:

    curl http://localhost/authors?select=id,name
    

    查询书籍id大于 100,且作者姓名在['x', 'y', 'z']中,并返回书籍的id, name和作者的id, name,并按照书籍id倒序排序。

    curl http://localhost/books?select=id,name,author{id,name}&id=gt.100&author.name=in.x,y,z&order=id.desc
    

    返回数据:

    {
        "count": 2,
        "object_list": [
            {
                "id": 2,
                "name": "Javascript",
                "author": {
                    "name": "x",
                    "id": 2
                }
            },
            {
                "id": 1,
                "name": "Python",
                "author": {
                    "name": "y",
                    "id": 1
                }
            }
        ],
        "is_paginated": false,
        "page": 1
    }
    

    有兴趣的可以直接查看项目中的 DEMO:https://github.com/dracarysX/django-rest-query/tree/master/demo

    PS

    目前仅仅实现了 Django orm 和 Peewee 的解析。感兴趣的同学可以多多提意见和 issue,谢谢。

    3 条回复    2017-06-28 17:40:00 +08:00
    AsterOcclu
        1
    AsterOcclu  
       2017-06-28 17:27:17 +08:00
    感觉类似 Graph API ?
    mooncakejs
        2
    mooncakejs  
       2017-06-28 17:29:01 +08:00
    楼主看过 postgrest 这个项目吗
    dracarysX
        3
    dracarysX  
    OP
       2017-06-28 17:40:00 +08:00
    @mooncakejs 嗯,就是看了这个项目才开始做的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1170 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:04 · PVG 07:04 · LAX 16:04 · JFK 19:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.