当前位置: 代码迷 >> .NET Framework >> django RestFrameWork的容易测试
  详细解决方案

django RestFrameWork的容易测试

热度:251   发布时间:2016-05-01 23:40:17.0
django RestFrameWork的简单测试

描述

写道
在icgoo中添加restframework,以订单模型Order做为资源制定接口
主要参考官方提供的接口例子:BlogPosts

?

官方例子:http://django-rest-framework.org/examples/blogpost.html

?

操作

settings中INSTALLED_APPS加入:

?

    'django.contrib.staticfiles',    'djangorestframework',    'restframework',

?

?

orders/resources.py

?

from djangorestframework.resources import ModelResourcefrom djangorestframework.reverse import reversefrom satchmo_store.shop.models import Orderfrom satchmo_store.contact.models import Contactclass ContactResource(ModelResource):    """    A Contact    """    model = Contact    fields = ('first_name', 'email')class OrderResource(ModelResource):    """    A Order Post     """    model = Order    fields = ('site', 'po_number', 'addressee', 'phone', 'url', 'city','contact')    ordering = ('-po_number',)    def url(self, instance):        return reverse('order',                        kwargs={'id': instance.id},                        request=self.request)    def contact(self, instance):        return reverse('contact',                        kwargs={'order': instance.id},                        request=self.request)

?

?

order/urls.py

?

from django.conf.urls.defaults import patterns, urlfrom djangorestframework.views import ListOrCreateModelView, InstanceModelViewfrom restframework.order.resources import OrderResource, ContactResourceurlpatterns = patterns('',    url(r'^$', ListOrCreateModelView.as_view(resource=OrderResource), name='order-root'),    url(r'^(?P<id>[^/]+)/$', InstanceModelView.as_view(resource=OrderResource), name='order'),    url(r'^(?P<order>[^/]+)/contact/$', ListOrCreateModelView.as_view(resource=ContactResource), name='contact'),    url(r'^(?P<order>[^/]+)/contact/(?P<id>[^/]+)/$', InstanceModelView.as_view(resource=ContactResource)),    )

?

?

测试页面

?

order list



?

?

order instance



?

?

接口的调用

写道
相当与每一个url就是一个接口函数,你可以有post,get,delete操作
如order list的url:http://127.0.0.1:8000/rest/order/
还有一个id为110的order实例的url:http://127.0.0.1:8000/rest/order/110/
写道
对于url:http://127.0.0.1:8000/rest/order/110/
相当于就是这个order实例的接口函数
现用curl命令测试该接口
通过get获取该对象的数据$curl -X get http://127.0.0.1:8000/rest/order/110/{"city": "", "po_number": "22", "site": {"domain": "store.icinfo.net", "name": "store.icinfo.net"}, "contact": "http://127.0.0.1:8000/rest/order/110/contact/", "phone": "22", "addressee": "22", "url": "http://127.0.0.1:8000/rest/order/110/"}你还可以获取不同的数据类型,如json,html,txt,xml$curl -X get http://127.0.0.1:8000/rest/order/110/?format=xml<root><city></city><po_number>22</po_number><site><domain>store.icinfo.net</domain><name>store.icinfo.net</name></site><contact>http://127.0.0.1:8000/rest/order/110/contact/</contact><phone>22</phone><addressee>22</addressee><url>http://127.0.0.1:8000/rest/order/110/</url></root>
通过Put修改对象数据$curl -X put -d "po_number=33&delivery_location=mainland&street1=22&contact=1&phone=11&addressee=22&country=22&site=1&unit=RMB" http://127.0.0.1:8000/rest/order/110/{"city": "", "po_number": "33", "site": {"domain": "demo.icgoo.net", "name": "IC\u8d2d\u5546\u57ce"}, "contact": "http://127.0.0.1:8000/rest/order/110/contact/", "phone": "11", "addressee": "22", "url": "http://127.0.0.1:8000/rest/order/110/"}
通过Delete删除对象$curl -X delete http://127.0.0.1:8000/rest/order/110/

?

?