python - PUT request to django tastypie resource not working -
i'm trying put request django tastypie resource in order update user info. now, can make post request put not working.
in api.py have this:
class userresource(modelresource): class meta: queryset = user.objects.all() resource_name = 'auth/user' fields = ['username', 'email'] authentication = basicauthentication() authorization = djangoauthorization() filtering = { "username": ('exact',), } class usersignupresource(modelresource): class meta: object_class = user resource_name = 'register_user' fields = ['username', 'email' , 'password'] allowed_methods = ['put','post'] authentication = basicauthentication() authorization = djangoauthorization() queryset = user.objects.all() def obj_create(self, bundle, request=none, **kwargs): bundle = super(usersignupresource, self).obj_create(bundle) and test i'me using this:
import urllib2, json def post(): def basic_authorization(user, password): s = user + ":" + password return "basic " + s.encode("base64").rstrip() data = json.dumps({'username':'david_000', 'email':'davidupdat@gmail.com', 'password':'xxx','provider':'twitter','extra_data':{'access_token':'xxx','id':'xxx'}}) req = urllib2.request("http://192.168.1.114:8080/api/stats/register_user/", headers = {"authorization": basic_authorization("xxx","xxx"),"content-type": "application/json"}, data = data) req.get_method = lambda:'put' f = urllib2.urlopen(req) post() i'm getting error:
traceback (most recent call last): file "test.py", line 18, in <module> post() file "test.py", line 16, in post f = urllib2.urlopen(req) file "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) file "/usr/lib/python2.7/urllib2.py", line 406, in open response = meth(req, response) file "/usr/lib/python2.7/urllib2.py", line 519, in http_response 'http', request, response, code, msg, hdrs) file "/usr/lib/python2.7/urllib2.py", line 444, in error return self._call_chain(*args) file "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain result = func(*args) file "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default raise httperror(req.get_full_url(), code, msg, hdrs, fp) urllib2.httperror: http error 400: bad request update:
i try this, gives me same error:
def obj_update(self, bundle, request=none, **kwargs): bundle = super(usersignupresource, self).obj_update(bundle) any idea? in advance!
this because trying issue put request, put request goes update record , not creating new one. tastypie try execute obj_update method , expects executed on instance. making request register_user/ url not object instance, doesnt make sense @ use put request url. put updating, not creating.
Comments
Post a Comment