为什么我使用tastypie POST请求获得NoneType HttpRequest

时间:2013-02-21 23:09:12

标签: django tastypie

在开发期间它在我的localhost上工作正常,但是当我在服务器上部署它时,我得到一个空请求。

class MyPinResource(Resource):
    pin = fields.CharField(attribute='pin', null=True)

    class Meta:
        resource_name = 'my-pin'
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()
        always_return_data = True
        include_resource_uri = False
        object_class = ApiObject
        allowed_methods = ['post']

    def detail_uri_kwargs(self, bundle_or_obj):
        if isinstance(bundle_or_obj, Bundle):
            return { 'domain': bundle_or_obj.obj }
        else:
            return { 'domain': bundle_or_obj }


    def obj_create(self, bundle, request=None, **kwargs):
        site_id = None
        if 'site_id' in request.GET and request.GET['site_id']:
            site_id = request.GET['site_id']
        else:
            LOG.error('site_id is required!')
            raise Exception

“error_message”:“'NoneType'对象没有属性'GET'”

AttributeError:'NoneType'对象没有属性'GET'

当调用request.GET

时,此错误在obj_create中

可能有什么不对?是否有空请求? 非常感谢帮助:)

3 个答案:

答案 0 :(得分:2)

正如我在评论中所写,问题可能是版本,结帐这个github issue。如果您使用的是0.9.12,则可以访问request中的bundle

答案 1 :(得分:0)

您可以通过文档here

中所述的bundle.request访问请求对象

答案 2 :(得分:0)

从pip安装的当前tastypie版本是0.9.12。在这个版本中,obj_create得到2个参数:

def obj_create(self, bundle, **kwargs):

而不是:

def obj_create(self, bundle, request=None, **kwargs):

在早期版本中。

现在您可以从bundle.request获取bundle请求。

相关问题