1

目前我有一个 Django 项目,我们称之为后端。有一个文件夹 api,我使用 Django-Tastypie 声明了这个资源:

from django.contrib.auth.models import User
from tastypie.resources import ModelResource, ALL
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization
from tastypie.cache import SimpleCache
from tastypie.throttle import CacheDBThrottle

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['email', 'password', 'is_staff', 'is_superuser']
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()
        cache = SimpleCache()
        throttle = CacheDBThrottle()

        filtering = {
            'username' : ALL,
            'date_joined' : ['gt','lt','gte','lte','range']        
        }

有了适当的路由规则,如果我访问类似的 URL http://127.0.0.1:8000/api/v1/user?format=json,我应该以 json 格式返回一些用户信息,但来自我的本地数据库。我不想使用任何本地数据库,除了可能使用一些虚拟数据进行测试。我希望这样的 get 请求能够使用我登录的会话的用户名和密码向某个远程服务器发出 SOAP 请求。

我已经有一个独立的 Python 应用程序,我可以在其中执行 SOAP 请求并使用SUDS和预下载的 WSDL 文件获取 SOAP 响应。现在我想在我的 Dhango 项目中包含此功能,即我更改项目中 settings.py 中的设置,但我不必修改项目内的应用程序。

4

2 回答 2

1

您可以定义自己的自定义管理器来使用您的独立 Python 应用程序。

于 2013-04-23T16:29:32.097 回答
0

来自美味的文档。将此代码添加到您的 urls.py 应该可以工作:

from tastypie.api import Api
from my_app.api.resources import UserResource

v1_api = Api(api_name='v1')
v1_api.register(UserResource())

#old urlpatterns here

urlpatterns += patterns('',
  (r'^api/', include(v1_api.urls)),
)
于 2013-04-25T08:07:20.173 回答