class Meta:
queryset = User.objects.all()
resource_name = 'user'
#limit 默认 获取数据数量 20
limit = 10
#不显示字段
# excludes = ['email','password',]
#需要显示的字段
fields = ['username', 'first_name', 'last_name', 'last_login', ]
#methods
allowed_methods = ['get', 'post', ]
#可排序字段 order_by = all_name
ordering = ['id', ]
#可过滤字段 同django一样
filtering = {
'username':ALL,
}
#API methods
url config
v1_api = Api(api_name='v1')
v1_api.register(EntryResource())
v1_api.register(UserResource())
urlpatterns = patterns('',
url(r'^api/',include(v1_api.urls)),
)
GET
#http://localhost:8000/api/v1/entry/
列表数据 url_name + api_name + model_name
#http://localhost:8000/api/v1/entry/1/
单数据 url_name + api_name + model_name + obj_id
#http://localhost:8000/api/v1/entry/set/1;3/
多个数据 url_name + api_name + model_name + set + 多个 obj_id
#http://localhost:8000/api/v1/entry/?title=keyword&?name__exact = keyword
filter过滤 参考django filter
#http://localhost:8000/api/v1/entry/?order_by=id
order_by排序 参考django order_by
POST
#curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post", "user": "/api/v1/user/1/"}'
# http://localhost:8000/api/v1/entry/
DELETE
delete obj
#curl --dump-header - -H "Content-Type: application/json" -X DELETE http://localhost:8000/api/v1/entry/4/
delete obj_list
#curl --dump-header - -H "Content-Type: application/json" -X DELETE http://localhost:8000/api/v1/entry/
#curl --dump-header - -H "Content-Type: application/json" -X DELETE http://localhost:8000/api/v1/entry/?id__in=13,15
#curl --dump-header - -H "Content-Type: application/json" -X DELETE http://localhost:8000/api/v1/entry/?body__contains=will
来源:oschina
链接:https://my.oschina.net/u/1272391/blog/382172