django-tastypie 笔记

拟墨画扇 提交于 2020-02-29 12:33:00

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!