Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何使用belongsTo关系创建一个记录(在ember.js中),并通过tastypie将其保存在django中?_Jquery_Json_Django_Ember.js_Tastypie - Fatal编程技术网

Jquery 如何使用belongsTo关系创建一个记录(在ember.js中),并通过tastypie将其保存在django中?

Jquery 如何使用belongsTo关系创建一个记录(在ember.js中),并通过tastypie将其保存在django中?,jquery,json,django,ember.js,tastypie,Jquery,Json,Django,Ember.js,Tastypie,所以我正在做一个聊天应用程序(前端在ember.js中,后端在django中),当然它必须发送消息 ember.js中的我的消息模型如下所示: Messages.Message = DS.Model.extend({ body: attr('string'), user: DS.belongsTo('user'), time: attr('date'), recipient: DS.belongsTo('recipient'), }); var store =

所以我正在做一个聊天应用程序(前端在ember.js中,后端在django中),当然它必须发送消息

ember.js中的我的消息模型如下所示:

Messages.Message = DS.Model.extend({
    body: attr('string'),
    user: DS.belongsTo('user'),
    time: attr('date'),
    recipient: DS.belongsTo('recipient'),
});
var store = this.store;

var message = store.createRecord('message', {
    body: 'Lorem ipsum'
});

store.find('recipient', 2).then(function(recipient) {
    message.set('recipient', recipient);
});

message.save();
我尝试使用以下示例中的代码创建并保存新消息:

因此,控制器中的saveMessage操作如下所示:

Messages.Message = DS.Model.extend({
    body: attr('string'),
    user: DS.belongsTo('user'),
    time: attr('date'),
    recipient: DS.belongsTo('recipient'),
});
var store = this.store;

var message = store.createRecord('message', {
    body: 'Lorem ipsum'
});

store.find('recipient', 2).then(function(recipient) {
    message.set('recipient', recipient);
});

message.save();
但生成的JSON属性“recipient”为空,因此服务器返回错误500,因为此字段不能为空

JSON应该如下所示:

{'body': 'loremipsum', 'recipient': {'username','some name'}, 'time': null, 'user': null}

你知道怎么做吗?

find
是异步的,并返回一个承诺。这意味着在调用
save
后,可以执行传递给
的匿名函数。将“保存”移动到“然后”,以便在保存之前已附加收件人

var store = this.store;

var message = store.createRecord('message', {
    body: 'Lorem ipsum'
});

store.find('recipient', 2).then(function(recipient) {
    message.set('recipient', recipient);
    message.save();
});