# Problems saving relationships using the Django Rest Framework

**URL:** <https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131>\
**Category:** Ember Data\
**Created:** [February 5, 2019, 3:32pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131 "2019-02-05T15:32:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 5, 2019, 3:32pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/1 "2019-02-05T15:32:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [February 5, 2019, 3:45pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/2 "2019-02-05T15:45:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 5, 2019, 3:47pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/3 "2019-02-05T15:47:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [February 5, 2019, 3:50pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/4 "2019-02-05T15:50:37Z")

</div>

Ah ok cool, so are you using [something like this](https://github.com/django-json-api/django-rest-framework-json-api) 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.

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 5, 2019, 4:09pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/5 "2019-02-05T16:09:16Z")

</div>

Hi, yes, am using rest\_framework\_json\_api. Everthing looks fine - just no idea what the problem is here in saving the relationship

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [February 5, 2019, 5:05pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/6 "2019-02-05T17:05:58Z")

</div>

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](https://stackoverflow.com/questions/36456328/django-throwing-incorrect-type-expected-url-string-on-api-call) 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.

---

<div class="post-metadata">

**Author:** ![zachgarwood](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/zachgarwood/32/13824_2.png) [@zachgarwood](https://discuss.emberjs.com/u/zachgarwood)\
**Post date:** [February 5, 2019, 9:15pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/7 "2019-02-05T21:15:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 6, 2019, 8:32am UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/8 "2019-02-06T08:32:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 6, 2019, 8:33am UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/9 "2019-02-06T08:33:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 6, 2019, 9:04am UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/10 "2019-02-06T09:04:11Z")

</div>

@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

```

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 6, 2019, 3:11pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/11 "2019-02-06T15:11:29Z")

</div>

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;
    }
});

```

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 7, 2019, 9:22am UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/12 "2019-02-07T09:22:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [February 7, 2019, 3:11pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/13 "2019-02-07T15:11:44Z")

</div>

Awesome glad you got it figured out!

---

<div class="post-metadata">

**Author:** ![therealbenhogan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/therealbenhogan/32/16287_2.png) [@therealbenhogan](https://discuss.emberjs.com/u/therealbenhogan)\
**Post date:** [February 7, 2019, 3:45pm UTC](https://discuss.emberjs.com/t/problems-saving-relationships-using-the-django-rest-framework/16131/14 "2019-02-07T15:45:33Z")

</div>

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