问题
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 :)
回答1:
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.
回答2:
You can access the request object through bundle.request as stated in documentation here
回答3:
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.
来源:https://stackoverflow.com/questions/15013990/why-am-i-getting-a-nonetype-httprequest-with-tastypie-post-request