【问题标题】:Tastypie - Calling dispatch of other resoueres ManuallyTastypie - 手动调用其他资源的调度
【发布时间】:2016-09-21 12:21:26
【问题描述】:

我需要创建一个连接到多个其他 API 的 API(名为 Dummy)以应用过滤器

我所做的是在主 API 的调度方法上调用调度方法。我为每个被调用的 API 更改了请求 QUERY_STRING

class DummyResource(MultipartResource, ModelResource):
    class Meta:
        queryset = Dummy.objects.all()
        resource_name = 'dummy'
        allowed_methods = ['get']

    def dispatch(self, request_type, request, **kwargs):
#Call the 1st API
        request.META['QUERY_STRING']='years_experience__in=2&production_2015__gt=105000'
        obj=APIONEResource()
        print 'Result'+str(obj.dispatch(request_type, request, **kwargs))

#Call the 2nd API    
        request.META['QUERY_STRING']='LOS__contains={2}&annual_production__in=4'
        obj=APITWOResource()
        print 'Result'+str(obj.dispatch(request_type, request, **kwargs))

    return super(DummyResource, self).dispatch(request_type, request, **kwargs)

现在,当我调用第一个 API 时,一切正常,我得到了正确的过滤结果。但是当我调用第二个 API 时,它会返回所有结果而没有任何过滤器。我可以确认 QUERY_STRING 已正确更改,并且无法理解为什么这不起作用。我尝试颠倒调用顺序,结果是第一次调用任何 API 时应用了过滤器,但第二次不应用。

有什么想法吗?

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    确定我做错了什么。我为所有调度方法重用了相同的请求对象。这意味着查询过滤器没有改变。

        def dispatch(self, request_type, request, **kwargs):
            import copy
            request2=copy.copy(request)
    
    #Call the 1st API
            request.META['QUERY_STRING']='years_experience__in=2&production_2015__gt=105000'
            obj=APIONEResource()
            print 'Result'+str(obj.dispatch(request_type, request, **kwargs))
    
    #Call the 2nd API    
            request2.META['QUERY_STRING']='LOS__contains={2}&annual_production__in=4'
            obj=APITWOResource()
            print 'Result'+str(obj.dispatch(request_type, request2, **kwargs))
    

    【讨论】:

      猜你喜欢
      • 2010-12-04
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多