1) PIP 安装: pip install django-tasypie
2) 添加到 APP: INSTALLED_APPS += ['tastypie’]
3) 创建一个 resources.py ,并将下面一段代码放置其中
from tastypie.resources import ModelResource
from App.models import History
class MyModelResource(ModelResource):
class Meta:
queryset = History.objects.all()
allowed_methods = ['get']
4)在urls.py中添加如下代码:
from django.conf.urls import include, url, patterns
from django.contrib import admin
from App.resources import MyModelResource
from tastypie.api import Api
v1_api = Api(api_name='v1')
v1_api.register(MyModelResource())
urlpatterns = patterns('',
# ...more URLconf bits here...
# Then add:
(r'^api/', include(v1_api.urls)),
)
进入你的项目位置比如:
cd workspace/a/pythonwork/Books
python manage.py runserver