Python 在django/tastype资源中传递请求变量
我需要在tastypie资源中执行过滤器查询。例如,输入应该是url的标题Python 在django/tastype资源中传递请求变量,python,django,rest,resources,tastypie,Python,Django,Rest,Resources,Tastypie,我需要在tastypie资源中执行过滤器查询。例如,输入应该是url的标题 new Ext.data.Store({ proxy: { url :'api/users/' type: "ajax", headers: { "Authorization": "1" } } }) 我在下面试过了 from tastypie.authorization import Authorization from django.contr
new Ext.data.Store({
proxy: {
url :'api/users/'
type: "ajax",
headers: {
"Authorization": "1"
}
}
})
我在下面试过了
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()
它是说,
名称“请求”未定义
。如何在ORM上传递筛选器?不确定为什么要在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)
我发现,
apply\u filter
非常有用。
我们可以像这样通过链接
http://localhost:8000/api/ca/entry/?format=json&userid=a7fc027eaf6d79498c44e1fabc39c0245d7d44fdbbcc9695fd3c4509a3c67009
代码
class ProfileResource(ModelResource):
class Meta:
queryset =ExProfile.objects.select_related()
resource_name = 'entry'
#authorization = Authorization()
#authentication = MyAuthentication()
filtering = {
'userid': ALL,
'homeAddress': ALL,
'email': ALL,
'query': ['icontains',],
}
def apply_filters(self, request, applicable_filters):
base_object_list = super(ProfileResource, self).apply_filters(request, applicable_filters)
query = request.META.get('HTTP_AUTHORIZATION')
if query:
qset = (
Q(api_key=query)
)
base_object_list = base_object_list.filter(qset).distinct()
return base_object_list