使用backbone.js& amp; Django + tastypie + mongoengine + 500错误保存模型骨干-tastypie.js

时间:2014-01-24 16:59:17

标签: javascript django backbone.js mongoengine tastypie

这是我尝试使用主干模型进行简单保存时收到的错误:

  

{“error_message”:“无法保存文档(不允许在_id上使用Mod)”,“traceback”:“Traceback(最近一次调用最后一次):\ n \ n File \”/ Users / mbp / Projects / env /employii/lib/python2.7/site-packages/tastypie/resources.py \“,第195行,在包装器中\ n response = callback(request,* args,** kwargs)\ n \ n File \”/ Users /mbp/Projects/env/employii/lib/python2.7/site-packages/tastypie/resources.py \“,第435行,在dispatch_detail \ n中返回self.dispatch('detail',request,** kwargs)\ n \ n File \“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie_mongoengine / resources.py \”,第390行,在dispatch \ n中返回self._wrap_request(request,lambda) :super(MongoEngineResource,self).dispatch(request_type,request,** kwargs))\ n \ n File \“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie_mongoengine / resources .py \“,第360行,在_wrap_request \ n中返回fun()\ n \ n文件\”/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie_mongoengine / resources.py \ “,林e 390,在\ n返回self._wrap_request(请求,lambda:super(MongoEngineResource,self).dispatch(request_type,request,** kwargs))\ n \ n File \“/ Users / mbp / Projects / env / employii /lib/python2.7/site-packages/tastypie/resources.py \“,第458行,在dispatch \ n response = method(request,** kwargs)\ n \ n File \”/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie / resources.py \“,第1408行,在put_detail \ n updated_bundle = self.obj_update(bundle = bundle,** self.remove_api_resource_names(kwargs))\ n \ n文件\“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie_mongoengine / resources.py \”,第561行,在obj_update \ n中返回self.save(bundle,skip_errors = skip_errors)\ n \ n File \“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie_mongoengine / resources.py \”,第578行,在save \ n return super(MongoEngineResource, self).save(bundle,skip_errors)\ n \ n File \“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / tastypie / resources.py \”,第2230行,在save \ n bundle.obj.save()\ n \ n文件\“../ thecontractbox / apps / staff / models.py \”,第811行,在save \ n return super(MHRBoxEmployee,self) .save(* args,** kwargs)\ n \ n File \“/ Users / mbp / Projects / env / employii / lib / python2.7 / site-packages / mongoengine / document.py \”,第294行,in save \ n raise OperationError(message%unicode(err))\ n \ n操作错误:无法保存文档(不允许在_id上使用Mod)\ n“}

骨干代码:

RAVE.Models.StaffModel = Backbone.Model.extend({
    urlRoot : '/m/api/v1/empployee',

    defaults: function() {
         return {
             active : true,
             visible : true
         };
    },
});

var StaffCollection = Backbone.Collection.extend({
    url : function(){
         return  '/m/api/v1/empployee/';
    },

    model: RAVE.Models.StaffModel,
});

这是从断开它的骨干视图调用的代码:

if( this.model.get('has_access') ){
     this.model.save({'has_access' : false});
}
else{
     this.model.save({'has_access' : true});
}

显然这是因为不允许修改模型ID但是tastypie正在尝试修改。我使用以下版本:

django-tastypie==0.10.0
django-tastypie-mongoengine==0.4.2
mongoengine==0.8.4
Django==1.5

1 个答案:

答案 0 :(得分:1)

好吧,我发现很难相信这是我能让它正常工作的唯一方法,但目前这是一个解决方案:

 if( this.model.get('has_access') ){
      this.model.save({'has_access' : false}, {patch : true});
 }
 else{
      this.model.save({'has_access' : true}, {patch : true});
 }

通过每次保存传递{patch:true},它只是部分更新模型。

我仍在等待任何其他解决方案,或者有人确认我当前的修复是正确的方法。