实现一款 no-sql 风格的参数解析框架rest-query,(like: /?select=id,name,author{id,name,school{*}})&id=gte.20&author.name=wwxiong&order=id.desc)。
目前实现了Django ORM和Peewee ORM
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
这样我们就实现了Author
和Book
的查询 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
目前仅仅实现了 Django orm 和 Peewee 的解析。感兴趣的同学可以多多提意见和 issue,谢谢。
1
AsterOcclu 2017-06-28 17:27:17 +08:00
感觉类似 Graph API ?
|
2
mooncakejs 2017-06-28 17:29:01 +08:00
楼主看过 postgrest 这个项目吗
|
3
dracarysX OP @mooncakejs 嗯,就是看了这个项目才开始做的。
|