简体   繁体   English

是否可以在Tastypie资源中请求其他api?

[英]Is it possible to request other api in a Tastypie resource?

I'm using Django 1.9.6 + Tastypie to implement RESTFUL api, there is an api that need to fetch data from api whitch is on another server, I don't know how to do it. 我正在使用Django 1.9.6 + Tastypie来实现RESTFUL api,有一个api需要从另一台服务器上的api whitch获取数据,我不知道该怎么做。

All the example I'd found is like this: 我发现的所有例子都是这样的:

from tastypie.resources import ModelResource
from services.models import Product
from tastypie.authorization import Authorization


class ProductResource(ModelResource):
    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        allowed_methods = ['get']
        authorization = Authorization()

The resource class fetch data from app's models(local database), is it possible to request an API which is on another server? 资源类从应用程序的模型(本地数据库)获取数据,是否可以请求另一台服务器上的API? If the answer is yes and then how to do it? 如果答案是肯定的,那该怎么办?

Maybe the question is stupid. 也许这个问题很愚蠢。

Thanks :) 谢谢 :)

Maybe what you are looking for are nested Resources or just the related resource fields, for instance: 也许您正在寻找的是嵌套资源或仅仅是相关的资源字段,例如:

from tastypie.resources import ModelResource
from tastypie import fields
from services.models import Product
from tastypie.authorization import Authorization


class ProductResource(ModelResource):
    shelf = fields.ForeignKey('shelf.api.ShelfResource', 'shelf', null=True)

    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        allowed_methods = ['get']
        authorization = Authorization()

The full=True will place the entire ShelfResource inside the ProductResource full=True会将整个ShelfResource放在ProductResource

EDITED EDITED

One way of implementing it could look like this: 实现它的一种方法可能如下所示:

import requests
from requests.exceptions import RequestException
[...]    

# implementing connection in models.py gives more flexibility.
class Product(models.Model):
    [...]

    def get_something(self):
        try:
            response = requests.get('/api/v1/other/cars/12341')
        except RequestException as e:
            return {'success': False, 'error': 'Other service unavailable!'}

        if response.status_code not in [200, 201, 202, 203, 204, 205]:

            return {'success': False, 'error': 'Something went wrong ({}): {}'.format(response.status_code, response.content)}
        data = json.load(response.content)
        data['success'] = True
        return data

    def something(self):
         self.get_something()


from tastypie.resources import ModelResource
from services.models import Product
from tastypie.authorization import Authorization


class ProductResource(ModelResource):
    something = fields.CharField('something', readonly=True)

    class Meta:
        queryset = Product.objects.all()
        resource_name = 'product'
        allowed_methods = ['get']
        authorization = Authorization()

Note that field wont' be serialized as JSON it's going to be escaped. 请注意,该字段不会被序列化为JSON,它将被转义。 Find out how to fix it there 找出如何解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM