dackyD
dackyD

Reputation: 267

Why am I getting a NoneType HttpRequest with tastypie POST request

It works fine on my localhost during development but when I deployed it on the server I get an empty request.

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' object has no attribute 'GET'"

AttributeError: 'NoneType' object has no attribute 'GET'

This error is inside obj_create when there is a call to request.GET

What could be wrong? Is it possible to have an empty request? Help would be much appreciated :)

Upvotes: 2

Views: 1376

Answers (3)

msc
msc

Reputation: 3800

As I wrote in my comment, the problem is probably the version, checkout this github issue. You can access the request in the bundle, if you're using 0.9.12.

Upvotes: 2

UnLiMiTeD
UnLiMiTeD

Reputation: 1040

Current tastypie version installed from pip is 0.9.12. In this version obj_create get 2 parametars:

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

instead of:

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

in the earlier versions.

Now you can get request from bundle with bundle.request.

Upvotes: 0

almalki
almalki

Reputation: 4775

You can access the request object through bundle.request as stated in documentation here

Upvotes: 0

Related Questions