django-tastypie 学习整理Quick Start

quick start:

配置环境(pip install):

1、建立model

2、应用中新建api.py

内容:

from tastypie.resources import ModelResource
from my_app.models import MyModel


class MyModelResource(ModelResource):
    class Meta:
        queryset = MyModel.objects.all()  
        allowed_methods = ['get']
     resource_name='mymodel' #若没有自动生成类名小写

3、主url配置:

# urls.py
from django.conf.urls import url, include
from myapp.api import EntryResource

entry_resource = EntryResource()

urlpatterns = [
    # The normal jazz here...
    url(r'^blog/', include('myapp.urls')),
    url(r'^api/', include(entry_resource.urls)),#EntryResource().urls
]

4、runserver localhost:8000

浏览器输入http://localhost:8000/api/mymodel/?format=json 输出有model对象

5、尝试更多地址访问(未添加授权,不支持PUT/DELET/POST操作):

posted on 2018-03-14 14:50  跑跑兔  阅读(225)  评论(0编辑  收藏  举报

导航