我正在尝试将Backbone.js模型作为POST发送到Piston并且收到错误:
Piston / 0.2.2(Django 1.3)崩溃报告:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/piston/handler.py",
line 81, in create inst = self.model.objects.get(**attrs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py",
line 132, in get return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",
line 341, in get clone = self.filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",
line 550, in filter return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py",
line 568, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py",
line 1172, in add_q can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py",
line 1060, in add_filter negate=negate, process_extras=process_extras)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py",
line 1238, in setup_joins "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'model' into field.
Choices are: calendar, date, id, name, priority
代码:
$(function() {
Backbone.emulateJSON = true;
$('form').submit(function() {
formdict = jsonform($("form#add_task :input"));
var new_task = new Task({
calendar: formdict.calendar,
date: formdict.date,
name: formdict.name,
priority: formdict.priority});
console.log("new _task: " + new_task);
new_task.save();
return false;
});
});
});
回复和请求信息:
Request URL:http://localhost:8000/api/task/
Request Method:POST
Status Code:500 INTERNAL SERVER ERROR
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:156
Content-Type:application/x-www-form-urlencoded
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
model:{"date":"2012-03-10T01:12:43.876Z","name":"New event","priority":0,"id":1,"calendar":"null"}
响应标题查看源
Access-Control-Allow-Headers:Origin,Content-Type,Accept
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin:*
Content-Type:text/html; charset=utf-8
Date:Sat, 10 Mar 2012 01:12:44 GMT
Server:WSGIServer/0.1 Python/2.7.2+
Vary:Authorization
如何让活塞接受该模型?
答案 0 :(得分:0)
您的错误代码显示:
FieldError: Cannot resolve keyword 'model' into field.
Choices are: calendar, date, id, name, priority
显然您发送错误的POST数据。打开调试控制台并检查save()函数发布的内容,看看是否匹配Piston所期望的内容。您可以在firebug / chrome控制台的网络选项卡中查看POST网址和参数。
答案 1 :(得分:0)
很明显你发了
{model:{"date":"2012-03-10T01:12:43.876Z","name":"New event","priority":0,"id":1,"calendar":"null"}}
而您的服务器端期望:
{“date”:“2012-03-10T01:12:43.876Z”,“name”:“新事件”,“优先级”:0,“id”:1,“calendar”:“null”}
因此,您要么将Backbone的sync()
更改为在JSON或服务器端不包含model
。
我不使用Django,只是简单地看https://bitbucket.org/jespern/django-piston/wiki/Documentation#!working-with-models给我的印象就是:它很简单:
class ModelHandler(BaseHandler):
fields = (('model', ('id', 'name', 'date', 'priority', 'calendar'),))