python - 在 django/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.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 上传递过滤器?

最佳答案

不确定为什么要在 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)

关于python - 在 django/tastypie 资源中传递请求变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798230/

相关文章:

python - pandas 根据多个条件迭代行,然后从列中减去?

python - 当您的数据不是均匀时间间隔时,是否有一种快速方法可以以均匀时间间隔对 Pandas Dataframe 进行滚动求和?

java - 使用过滤器将请求转发到另一个休息服务

rest - 在 RESTful API 设计中获取用户配置文件的最佳实践是什么

python - 找出数字组合中的最高总和

python - 当在源中找不到 URL 时,从 Google 本地搜索中提取网站(URL)吗?

python - 插入不是默认 DJANGO 的数据库

python - 优化 Django 中的代码——将 ManyToMany 视为矩阵

python - 在使用 django-filter 进行过滤时如何将过滤后的值导出到 csv 文件中

java - 通过 Java/jersey REST 资源访问 mySQL 数据库