Problems saving relationships using the Django Rest Framework


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'),

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:

let post = $'post',1);
let comment = $'comment',{description:'some text here',post:post});;

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

Hi @therealbenhogan, what are you using for your adapter for these models? Are you using ember-django-adapter? Or a hand-rolled REST adapter of some kind? My guess is your issue involves the adapter somewhere.

Hi - thanks for getting back. Sorry, forgot to mention - using DS.JSONAPIAdapter

Ah ok cool, so are you using something like this on the backend or manually formatting to JSONAPI or what? We use JSONAPI and that lib on top of DRF at my company and it works really well for us.

Hi, yes, am using rest_framework_json_api. Everthing looks fine - just no idea what the problem is here in saving the relationship

Ah ok well it sounds like from an Ember perspective everything should be set up properly and your backend and frontend should have the correct “shared language” as it were. I’m a lot more competent with Ember than Django myself but based on a bit of poking around this stackoverflow post sounds pretty similar… Sorry hate to just send you links to other stuff but like I said your Ember setup looks solid and I use Django but it’s not my forte.

1 Like

I believe the HyperlinkedModelSerializer expects to identify models using a url (like /api/posts/123), but Ember is sending just the id (123). I don’t think HyperlinkedModelSerializer is necessary here, I believe you can just use a regular ModelSerializer.

Many thanks - I’ll take a look at the link and let you know if I solve it!

Hi @zachgarwood - many thanks for getting back. I have tried the ModelSerializer too, but with no joy, but will give it another try this morning and will let you know if that solves the issue.

@zachgarwood @dknutsen Hi, I’ve tried with ModelSerializer again - still no joy. Here is the sample JSON from the console. I am passing the pk/ID as an integer?

        description: sample text

Hi, I have forced this to work by creating a comment serializer as shown below but as you can see I am hard coding the post ID to which this comment belongs to. Is this the correct approach? If it is then how do I grab the value of the post ID?

from serializers/comment.js

  import DS from 'ember-data';
  export default DS.JSONAPISerializer.extend({
        ...omitted for brevity ....
    serialize(snapshot, options) {
	let json = this._super(...arguments); = 1; // how to grab the value of the post ID?
	return json;

Hi @dknutsen @zachgarwood - I have solved it. It wasn’t a problem with the data I was passing to Django. I was using the plain rest_framework to serialize the data in Django, when I should have been importing the rest_framework_json_api serializer!

Many thanks for your help - it was much appreciated.

Awesome glad you got it figured out!

Hi - a case of not seeing the woods for the trees !!! Thanks again for your help