djang-tastypie学习整理

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操作):

上一篇:【搬运工】linux下创建用户(一)


下一篇:EffectiveJava阅读笔记(一)