2012-03-10 62 views
0

我試圖發送一個Backbone.js的模型作爲POST到活塞和我得到一個錯誤:無法將關鍵字'model'解析爲字段。活塞與Backbone.js的

活塞/ 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

什麼是發送到服務器的請求?這顯然是服務器端錯誤,您能否與我們分享相關部分? – Tadeck 2012-03-10 01:13:04

+0

附加請求。 – user784756 2012-03-10 01:16:19

回答

0

那麼顯然您發送

{model:{"date":"2012-03-10T01:12:43.876Z","name":"New event","priority":0,"id":1,"calendar":"null"}} 

,而你的服務器端預計:

{ 「日期」: 「2012-03-10T01:12:43.876Z」, 「名」:「新事件「,」優先級「: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'),)) 
0

你的錯誤代碼說:

FieldError: Cannot resolve keyword 'model' into field. 
    Choices are: calendar, date, id, name, priority 

顯然你發送錯誤的POST數據。打開調試控制檯並檢查save()函數發佈的內容,看看它是否符合Piston的期望。您可以在Firebug/Chrome控制檯的網絡選項卡中看到POST URL和參數。

+0

發佈模型: model:{「date」:「2012-03-10T01:12:43.876Z」,「name」:「New event」,「priority」:0,「id」 「calendar」:「null」} – user784756 2012-03-10 01:18:00

+0

您的服務器獲取的JSON爲{model:{calendar:'',priority:'',etc ...}}但似乎只期望爲{calendar:'',priority: ''等等。}。更改您的活塞代碼以僅讀取model.calendar而不是日曆 – Guumaster 2012-03-10 01:19:30

+0

我知道。我不知道如何擺脫模型。我認爲用骨幹模型發佈活塞是可能的。 – user784756 2012-03-10 01:21:24

相關問題