【问题标题】:Is it possible to request other api in a Tastypie resource?是否可以在 Tastypie 资源中请求其他 api?
【发布时间】:2016-06-02 08:03:02
【问题描述】:

我正在使用 Django 1.9.6 + Tastypie 来实现 RESTFUL api,有一个 api 需要从 api 获取数据,而在另一台服务器上,我不知道该怎么做。

我找到的所有例子都是这样的:

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()

资源类从应用程序的模型(本地数据库)中获取数据,是否可以请求另一个服务器上的 API?如果答案是肯定的,那该怎么做呢?

也许这个问题很愚蠢。

谢谢:)

【问题讨论】:

    标签: python django rest tastypie


    【解决方案1】:

    也许您正在寻找的是nested Resources 或只是相关的资源字段,例如:

    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()
    

    full=True 会将整个ShelfResource 放在ProductResource

    【讨论】:

      【解决方案2】:

      已编辑

      一种实现方式可能如下所示:

      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()
      

      请注意,字段不会被序列化为 JSON,而是会被转义。了解如何修复它there

      【讨论】:

      • 非常感谢您迟到接受,抱歉
      • @bartosz 关于现代 API 后端应该如何工作的观点只是一种观点。现代(无服务器)应用程序是高度事件驱动的:一个前端触发器可能具有后端挂钩的级联效果。说前端客户端必须启动所有 API 活动与现代软件开发脱节。
      • 没错,这里不需要我的意见
      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2011-12-10
      • 2020-10-26
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多