Search code examples
javascriptpythondjangobackbone.jsdjango-piston

Cannot resolve keyword 'model' into field. Piston with Backbone.js


I am trying to send a Backbone.js model as a POST to Piston and am getting an error:

Piston/0.2.2 (Django 1.3) crash report:

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

The code:

$(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;
});

});

});

Response and Request Information:

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"}

Response Headersview source

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

How do I get Piston to accept the model?


Solution

  • Well clearly you send

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

    whereas your server side expects:

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

    So either you change Backbone's sync() to not include model in JSON or your server side. I don't use Django but simply looking at https://bitbucket.org/jespern/django-piston/wiki/Documentation#!working-with-models gives me the impression that it's as simple as:

    class ModelHandler(BaseHandler):
       fields = (('model', ('id', 'name', 'date', 'priority', 'calendar'),))