django-tastypie 筆記

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 django

#curl --dump-header - -H "Content-Type: application/json" -X DELETE http://localhost:8000/api/v1/entry/?body__contains=will json

相關文章
相關標籤/搜索