在django/tastypie资源中传递请求变量

sum*_*mit 2 python django rest resources tastypie

我需要在tastypie资源中执行过滤器查询.输入应该是url的标题,例如

new Ext.data.Store({
   proxy: {
     url :'api/users/'
     type: "ajax",
      headers: {
       "Authorization": "1"
    }
   }
 })  
Run Code Online (Sandbox Code Playgroud)

我在下面试过

from tastypie.authorization import Authorization
from django.contrib.auth.models import User
from tastypie.authentication import BasicAuthentication
from tastypie import fields
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie.validation import Validation
from userInfo.models import ExProfile

class UserResource(ModelResource,request):
        class Meta:
            queryset = User.objects.filter(id=request.META.get('HTTP_AUTHORIZATION'))
            resource_name = 'user'
            excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
            authorization = Authorization()
            authentication=MyAuthentication()
Run Code Online (Sandbox Code Playgroud)

它在说name 'request' is not defined.如何在ORM上传递过滤器?

Tib*_*vin 5

不确定为什么要在UserResource中继承请求.

我需要做这样的事情,我可以提出的最佳解决方案是覆盖调度方法.像这样

class UserResource(ModelResource):
   def dispatch(self, request_type, request, **kwargs):
        self._meta.queryset.filter(id=request.META.get('HTTP_AUTHORIZATION'))
        return super(UserResource, self).dispatch(request_type, request, **kwargs)
Run Code Online (Sandbox Code Playgroud)