在 django/tastypie 资源中传递请求变量 [英] passing request variables in django/tastypie resources
本文介绍了在 django/tastypie 资源中传递请求变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在美味派资源中执行过滤查询.输入应该是 url 的标题,例如
I need to execute filter query inside tastypie resources. the input should be the headers of url for example
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()
它是说name 'request' is not defined
.如何在 ORM 上传递过滤器?
it is saying name 'request' is not defined
. How to pass filters on ORM them?
推荐答案
不知道为什么要继承 UserResource 中的请求.
Not sure why are you inheriting request in UserResource.
我需要做这样的事情,我能想到的最佳解决方案是覆盖 dispatch 方法.像这样
I needed to do something like this and the best solution I could come up was to overwrite the dispatch method. Like this
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)
这篇关于在 django/tastypie 资源中传递请求变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文