django-tastypie 学习整理Quick Start
quick start:
配置环境(pip install):
- Python 2.7+ or Python 3.4+
- Django 1.8+
python-mimeparse
0.1.4+ (http://pypi.python.org/pypi/python-mimeparse)dateutil
(http://labix.org/python-dateutil)
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操作):
- http://127.0.0.1:8000/api/entry/?format=json
- http://127.0.0.1:8000/api/entry/1/?format=json
- http://127.0.0.1:8000/api/entry/schema/?format=json
- http://127.0.0.1:8000/api/entry/set/1;3/?format=json