当前位置: 首页 > 工具软件 > Tastypie > 使用案例 >

djang-tastypie 学习整理Quick Start

隗锐进
2023-12-01

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

转载于:https://www.cnblogs.com/EmptyRabbit/p/8567526.html

 类似资料: