Problems saving relationships using the Django Rest Framework


#1

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


#2

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.


#3

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


#4

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.


#5

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


#6

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.


#7

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.


#8

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


#9

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.


#10

@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?

data:{…}
    attributes:{…}
        email: this@that.com
        description: sample text
    relationships:{…}
        post:{…}
           data:{…}
                id:1
                type:posts
    type:comments

#11

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);
	json.data.attributes.post = 1; // how to grab the value of the post ID?
	return json;
    }
});

#12

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.


#13

Awesome glad you got it figured out!


#14

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