Hi,
Having spent some time away from Ember, concentrating on Django, I am now trying to save some simple relationships. The backend is provided by the Django rest framework. Not having any issues saving simple models (with no relationships) but have now come against a road bump, which I’m really struggling to fix!
I have a post model with a hasMany relationship with comments, which belongTo posts.
The comment model:
export default DS.Model.extend({
description: DS.attr('string'),
post:DS.belongsTo('post')
});
The post model:
export default DS.Model.extend({
name: DS.attr('string'),
postComments: DS.hasMany('comment')
});
Everything looks fine with the Django rest API; from there I can save new comments and assign them to the correct post. The serializer I’m using for comments is as follows:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = ('id','url','description','post')
Turning to Ember, using the Inspector, assuming I already have a post saved, I do:
$E.store.findRecord('post',1);
let post = $E.store.peekRecord('post',1);
let comment = $E.store.createRecord('comment',{description:'some text here',post:post});
comment.save();
When I do this I get this error message in the console:
Error: Ember Data Request POST http://localhost:8000/api/comments returned a 400
and looking at the POST request I see
{"errors":[{"detail":"Incorrect type. Expected URL string, received dict.","source":{"pointer":"/data/attributes/post"},"status":"400"}]}
In the Django console, I see a bad request to /api/comments
I hope you can help!
Many thanks