python - Unable to PUT or PATCH with Tastypie -
update: i'm able put/update on info in backwards manner - seems more bug or security hole me?
projectviewmodel.prototype.edit = function (projectid, project) { var _project = json.parse(project); _project.id = projectid; _project.name = "test 123!"; project = json.stringify(_project); $.ajax({ url: "http://" + window.location.href.split("/")[2] + "/api/v1/project/", type: "post", data: project, contenttype: "application/json", processdata: false, success: function (data) { $("#div_response").text("updated!"); }, error: function (data) { $("#div_response").text(data.responsetext); } }); };
the code above allows me post new object tastypie, , if include objects id value, update existing object rather create new one.
original q:
i able , post unable set or patch tastypie. here's @ api:
from tastypie.resources import modelresource tastypie.authentication import authentication tastypie.authorization import djangoauthorization, authorization tastypie import fields projecttrackerserver.projects.models import project projecttrackerserver.milestones.models import milestone class projectresource(modelresource): # namespace-to-milestones, related_name-from-milestones-model, show-full milestones = fields.tomanyfield('projecttrackerserver.projects.api.milestoneresource', 'projects', full=true) class meta: queryset = project.objects.all() resource_name = 'project' allowed_methods = ['get', 'post', 'put', 'delete', 'patch'] authentication = authentication() authorization = authorization() class milestoneresource(modelresource): project = fields.toonefield('projecttrackerserver.projects.api.projectresource', 'project') class meta: queryset = milestone.objects.all() resource_name = 'milestone' allowed_methods = ['get', 'post', 'put', 'delete', 'patch'] authentication = authentication() authorization = authorization()
and quick review of javascript ajax code, 'projectid' argument method:
//this test using put/patch fetching project, //updating it, , saving server. $.ajax({ url: "http://" + window.location.href.split("/")[2] + "/api/v1/project/" + projectid + "/", type: "get", data: "", contenttype: "application/json", processdata: false, success: function (data) { var project = data; project.name = "project y!"; project.slug = "project-y"; var jsondata = json.stringify(project); $.ajax({ url: "http://" + window.location.href.split("/")[2] + "/api/v1/project/" + projectid + "/", type: "patch", data: jsondata, contenttype: "application/json", processdata: false, crossdomain: true, headers: { "x-http-method-override": "patch" }, success: function (data) { $("#div_response").text(data.responsetext); }, error: function(data) { $("#div_response").text(data.responsetext); } }); }, error: function (data) { $("#div_response").text(data.responsetext); } });
the curious thing not receive error. if seek output "data", using json.stringify(), double-quotes ("") result.
if helps, i'm including models well.
from django.db import models django.template.defaultfilters import slugify class project(models.model): name = models.charfield(max_length=200) start_date = models.datefield() end_date = models.datefield() pm_id = models.integerfield() status = models.integerfield() slug = models.slugfield() def __unicode__(self): homecoming self.name def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.name)[:50] homecoming super(project, self).save(*args, **kwargs)
update: i'm able , post fine, i'm not sure why curl responding cannot resolve host. result using curl:
c:\curl>curl --dump-header - -h "content-type: application/json" -x set --data {{"end_date": "2014-10-10", "id": "16", "milestones": [], "name": "project xy! "resource_uri": "/api/v1/project/16/", "slug": "project-x", "start_date": "20 -12-30", "status": 1}}' http://localhost:1231/api/v1/project/16 curl: (6) not resolve host: (nil); no info record of requested type curl: (6) not resolve host: (nil); host not found curl: (6) not resolve host: (nil); no info record of requested type curl: (6) not resolve host: (nil); host not found curl: (3) [globbing] illegal character in range specification @ pos 2 curl: (6) not resolve host: (nil); host not found curl: (6) not resolve host: (nil); no info record of requested type curl: (6) not resolve host: (nil); host not found curl: (3) <url> malformed curl: (6) not resolve host: (nil); host not found curl: (6) not resolve host: (nil); no info record of requested type curl: (6) not resolve host: (nil); host not found curl: (6) not resolve host: (nil); no info record of requested type curl: (6) not resolve host: (nil); host not found curl: (3) [globbing] unmatched close brace/bracket @ pos 2 http/1.0 301 moved permanently date: wed, 20 feb 2013 19:58:56 gmt server: wsgiserver/0.1 python/2.7.3 access-control-allow-origin: * access-control-allow-methods: post,get,options,put,delete content-type: text/html; charset=utf-8 location: http://localhost:1231/api/v1/project/16/ access-control-allow-headers: origin,content-type,accept
python django tastypie
No comments:
Post a Comment